I have problems using pthread_create if passing a class method to an argument, because the compiler constantly produces an error:

cannot convert 'void* (Server::*)(void*)' to 'void* (*)(void*)' for argument '3' to 'int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)' Server.cpp 

I have a Server class, it has a private member pthread_t receivingThread_; the public function void myFunc1() and another public function void* myFunc2(void *args) which returns 0.

Here is the function description, where I make the call to pthread_create ()

 myFunc1(){ pthread_create(&receivingThread_,NULL,&Server::myFunc2,NULL); } 

However, it does not work, most likely somewhere is a common mistake, but after reading other questions, I cannot understand how to fix my own. Thanks in advance for your help.

  • class method accepts hidden this parameter. Even if you type types, pthread_create cannot pass this this to it. maybe a static method could be used. For now, I only see a crutch from the usual function outside the class - Mike

1 answer 1

To call a non-static member of a class without specifying a class object is nonsense.

If it were standard for C ++ std::thread , then it could be passed to the first parameter of this and he would have joyfully earned - he is smart enough to understand such things ...

But since pthreads, by and large, is a C-library, a workaround is needed - creating a free function that will call a member function for a particular object.