create_thread for class member ?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > create_thread for class member ?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    create_thread for class member ?  
vertigo


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-28-04 12:48 PM

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::StartServerLoo
p,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






[ Post a follow-up to this message ]



    Re: create_thread for class member ?  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:44 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register