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:

struct Obj{
  int i;
  Obj( int n ): i(n){}
  void* myfun(void* ptr){}

Obj o(6);
pthread_create( handler, NULL, &(o.myfun), NULL);

Thank you

7 Years
Discussion Span
Last Post by rmlopes

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

pthread_create( &handler, NULL, o.myfun, NULL);
Votes + Comments
Not helpfull at all. The poster didn't even read correctly, apparently

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.