|
Home > Archive > Unix Programming > August 2004 > create_thread for class member ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
create_thread for class member ?
|
|
| vertigo 2004-08-28, 7:48 am |
| 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
when i changed to:
pthread_create(&ServerLoopThread,NULL,(void*(*)(void*))&Node::StartServerLoop,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 ?
How can i correct it ?
Thanx
Michal
| |
| David Schwartz 2004-08-29, 2: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::StartServerLoop,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
|
|
|
|
|