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

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.