08-29-04 07:49 AM
"vertigo" <none@microsoft.com> wrote in message
news:cgpkmm$ssm$2@nemesis.news.tpi.pl...
This question is asked about once a month.
> Hello
> I have class Node and function:
> void Node::Start(void){
> pthread_create(&ServerLoopThread,NULL,&Node::StartServerLoop,NULL);
> }
>
> void Node::StartServerLoop(void){
> ...
> }
>
> and durring compilation i receive error:
> Node.c: In member function `void Node::Start()':
> Node.c:21: error: cannot convert `void (Node::*)()' to `void*(*)(void*)'
> for
> argument `3' to `int pthread_create(pthread_t*, const pthread_attr_t*,
> void*(*)(void*), void*)'
> make: *** [Node.o] Error 1
This is because 'pthread_create' is not a C++ function and does not want
a pointer to a C++ function. It wants a pointer to a function with C
linkage.
> when i changed to:
>
> pthread_create(&ServerLoopThread,NULL,(void*(*)(void*))&Node::StartServerL
oop,NULL);
> i receive error:
> Node.c: In member function `void Node::Start()':
> Node.c:20: error: converting from `void (Node::*)()' to `void*(*)(void*)'
> make: *** [Node.o] Error 1
>
> Why ?
You must pass pthread_create a pointer to the type of function it wants,
and in order to do that you must have a function of the type that it wants.
> How can i correct it ?
The textbook way to do it is to create a function with C linkage that is
*not* a member of the class (but it can be a friend of the class) and pass
pthread_create a pointer to that function. You can cast the 'this' pointer
to a 'void *' and then pass it to that function.
Googling for "C++ class member pthread_create" produced tons of hits.
Just ignore the ones that don't have an 'extern "C"' in them, because they
are *wrong*.
DS
[ Post a follow-up to this message ]
|