943,717 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 508
  • C++ RSS
Jun 16th, 2009
0

pthread issue with the function to be run

Expand Post »
Hi all,

I am new to threads in c++, and I must use the pthread library. The documentation is easy to understand, but there is a more general aspect that I am missing. My question is: Is it possible to give a pointer to an object's function as argument to pthread_create()?
I tried, without success, something like this:
cpp Syntax (Toggle Plain Text)
  1. struct Obj{
  2. int i;
  3. Obj( int n ): i(n){}
  4. void* myfun(void* ptr){}
  5. }
  6.  
  7. Obj o(6);
  8. pthread_create( handler, NULL, &(o.myfun), NULL);

Thank you
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
rmlopes is offline Offline
31 posts
since Sep 2008
Jun 17th, 2009
0

Re: pthread issue with the function to be run

You have to mention the name of function and address of handler with pthread_create arguments.

C++ Syntax (Toggle Plain Text)
  1. pthread_create( &handler, NULL, o.myfun, NULL);
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 17th, 2009
1

Re: pthread issue with the function to be run

I regret.
I didn't notice : Is it possible to give a pointer to an object's function.
You cann't use instance member function's address with pthread_create function. It must be static.

You have to use & (an address operator) with handler variable and do not put & (address operator) with o.myfun (which is 3rd argument). - but the must be static.
Last edited by adatapost; Jun 17th, 2009 at 6:07 am.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 17th, 2009
0

Re: pthread issue with the function to be run

Hi all,

Thank you adatapost for your attention and reply.
Just a while ago I found http://www.parashift.com/c++-faq-lit...o-members.html in which point 33.2 explicitly explains my situation.

Rui
Reputation Points: 10
Solved Threads: 0
Light Poster
rmlopes is offline Offline
31 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Correct the plz
Next Thread in C++ Forum Timeline: best open source c++ compiler





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC