Google Gruppi non supporta più i nuovi post o le nuove iscrizioni Usenet. I contenuti storici continuano a essere visibili.

Threading with Classes

5 visualizzazioni
Passa al primo messaggio da leggere

Stefan Siefer

da leggere,
23 ago 2001, 09:57:1423/08/01
a
Hi,

I have a question concerning threading an Classes.

First I will describe you the present state of my Program.
My Main Prog is a C++ Class, which controlls different subclasses. I have a
private memberfunction called handle_client. Threading in this class fails,
what I try is this:
class cMain
{
....
int main()
{
res = pthread_create(&handle_thread, NULL, handle_client,(void * )
fd_temp);
....
}
void * handle_client(void * arg)
{
...
}

}

what I get is this:

no matches converting function `handle_client' to type
`void *(*) (void *)'
candidates are: void *cMain::handle_client (void *)

If I try the same with a global function handle_client (e.g. not a class
member) this works fine.

IMHO this looks to me like I can't thread within a class with an private
class function. What I would like to know, is why is this like it is? Can
somebody explain this to me or give me a hint or link where I can get Infos
about?

Thx,

Stefan Siefert

David Schwartz

da leggere,
23 ago 2001, 14:56:2723/08/01
a
Stefan Siefer wrote:

> IMHO this looks to me like I can't thread within a class with an private
> class function. What I would like to know, is why is this like it is? Can
> somebody explain this to me or give me a hint or link where I can get Infos
> about?

You are correct, you can't. The reason is that thread start functions
are defined to have C linkage and take a single pointer. What's to
explain? Create a global "C" function that's a friend of the class.

DS

Wim Lauwers

da leggere,
24 ago 2001, 03:10:4024/08/01
a


And if you really _must_ have the object executing inside the thread,
use a static class function to start the thread, pass it the pointer to
your object as argument and launch the correct function from there,
using this object pointer. A friend is probably cleaner though.

Wim

Stefan Siefer

da leggere,
24 ago 2001, 10:09:0124/08/01
a
Thx for the suggestions. I did solve the problem this way. But what I would
like to understand is what the technical problem is why this above doesn't
work (I'm not sure but I thought I have done this long time ago in
Java...).

Thx,

Stefan Siefert

Alan Veasey

da leggere,
25 ago 2001, 04:04:3025/08/01
a
Stefan Siefer <S.Si...@global-media.de> wrote in message news:<3b865fe6$0$283$4dbe...@businessnews.de.uu.net>...

Hi. I hope this helps. I too just started using threads and classes.
My attempt is to create a thread class that hides the pthreads
functions with more generic names (start, stop, cancel, etc). I don't
fully understand the technical part but here goes: pthread_create
wants a pointer to a function that returns void * and takes void * as
a parameter. The problem with passing a member function to it is that
the function name is actually class::function instead of just
function. Making the member function static somehow gets around. From
what I have read, making a member function static removes the this
pointer from being passed to it. My accomplishments sped up after I
found a thread class called ObjectThread. It can be found at
http://sourceforge.net/projects/objectthread. Dude, this source rocks.
I learned MUCH more about C++ looking over the source. There may be
better and to the experts this may not be very good, but it could be a
lifesaver. Anyway, I hope this helps and good luck.

Řyvind Rřtvold

da leggere,
25 ago 2001, 05:44:3925/08/01
a
Stefan Siefer <S.Si...@global-media.de> writes:

[snip]


> (I'm not sure but I thought I have done this long time ago in
> Java...).

But ofcourse - Java has a object based threading model. If you can
find a object based threading library for C++ (or make it yourself)
you could do something like this.

--
Ųyvind

David Butenhof

da leggere,
5 set 2001, 14:28:1605/09/01
a
Alan Veasey wrote:

> I don't
> fully understand the technical part but here goes: pthread_create
> wants a pointer to a function that returns void * and takes void * as
> a parameter. The problem with passing a member function to it is that
> the function name is actually class::function instead of just
> function. Making the member function static somehow gets around. From
> what I have read, making a member function static removes the this
> pointer from being passed to it.

That may be true on SOME implementations of C++, but it's not "correct"
C++, and is likely to get you into trouble.

The actual requirement by pthread_create() is that the function you're
asking it to call in the context of a new thread must have C calling
conventions. While most C++ implementations do NOT use calling conventions
compatible with C for normal member functions, many DO for static member
functions. Some, on the other hand, don't, and there's no reason they
should. (That is, ANSI C++ says they needn't.)

The only proper way to do this is to specify, to pthread_create(), the name
of a function declared with the 'extern "C"' C++ qualification. This will
force the C++ compiler to generate a routine that expects to be called with
the platform's C calling conventions, and it'll always work with
pthread_create(). You can pass to that routine a "(void*)this", which your
'extern "C"' "trampoline" can then convert back into a C++ object pointer
before passing it on to your member function.

/------------------[ David.B...@compaq.com ]------------------\
| Compaq Computer Corporation POSIX Thread Architect |
| My book: http://www.awl.com/cseng/titles/0-201-63392-2/ |
\-----[ http://home.earthlink.net/~anneart/family/dave.html ]-----/

0 nuovi messaggi