   Pthread Question
  Hello Folks,No compiling error for the following pthread code:void* worker1(void* ) {}int main() {pthread_t ThreadA;pthread_create(&ThreadA, NULL, worker1, NULL);// ...}but I got "error: argument of type ‘void* (myClass::)(void*)’ does not match ‘void* (*)(void*)" in the ctor:class myClass {   pthread_t ThreadA;void* worker1(void* ) {}public: myClass() {pthread_create(&ThreadA, NULL, worker1, NULL);// compiling error }};Does anybody know how to make it run in the ctor? Thanks in advance,Robert   
  Why are the functions returning pointer to void, instead of just void?Quote:but I got "error: argument of type ‘void* (myClass::)(void*)’ does not match ‘void* (*)(void*)" in the ctor:Make worker static function. Member functions are different and require 'this' to be passed as well. pthreads is a C api, and doesn't know about member functions.Or, pass via a proxy function:void invoke(void * p) {  MyClass * pp = (MyClass)(p);  pp->worker();}pthread_create(&ThreadA, NULL, invoke, pMyClass);;  One problem is that in C++, functions that are defined in a class are of a different type from those defined as "free functions". You can't take a function pointer that is intended to point at one and point it to the other. That's partially the issue that your compiler is complaining about. You'll have to define your worker thread function outside of your class if you want to use it as a pthread.edit: Antheus, the function passed to pthread is supposed to return a void*. See the man page for pthread_create.   ;Quote:Original post by AntheusWhy are the functions returning pointer to void, instead of just void?Quote:but I got "error: argument of type ‘void* (myClass::)(void*)’ does not match ‘void* (*)(void*)" in the ctor:Make worker static function. Member functions are different and require 'this' to be passed as well. pthreads is a C api, and doesn't know about member functions.Or, pass via a proxy function:void invoke(void * p) {  MyClass * pp = (MyClass)(p);  pp->worker();}pthread_create(&ThreadA, NULL, invoke, pMyClass);You can also make the proxy function a static member function of the class like this:static void StaticWorker1(void* p)  {    ((MyClass *)p)->Worker1();  }And create the task (or tasks) from a ordinary member function like this.MyClass::CreateTasks(){  pthread_create(&ThreadA,NULL,StaticWorker1,this);}This way you can keep everything in the class..and your Worker1() is a ordinary member function.   ;  Thanks a lot folks, especially mrr. Since in the worker1() mutex is used, I can not make worker1 a static member function. mrr's proxy function solution works. So the code would be:class myClass {   pthread_t ThreadA;   static void* StaticWorker1(void* p) {(static_cast<myClass* >(p))->worker1(p);}void* worker1(void* X ) {//...}public: myClass() {pthread_create(&ThreadA, NULL, StaticWorker1, this); }};;  Yes something like that.But as I understand you should avoid things that can return error codes in constructors.   ;  If you get into a brain overload you may want to try it this wayCode the pThread "C" stuff in one file like thisand then put your C++ code in a different file. // Requires compiler option -lpthreadvoid *MyCPP(void *threadid);// pthread stuff#define NUM_THREADS 5  // for pthreadslong NumberPthreads =0 ;    //# of threads in existancepthread_t threads[NUM_THREADS];PthreadWrapper(){    printf ( "In main: Creating CPP thread \n" );    NumberPthreads ++;    ReturnCode = pthread_create(&threads[NumberPthreads], NULL, MyCPP , (void *)NumberPthreads);    if ( ReturnCode )    {   printf ( "ERROR; return code from pthread_create() is %d\n", ReturnCode );   exit ( -1 );    }    return;}  // end of Main!MyCPP(){// Code the C++ in a separate file from this oneCallCppStuff();pthread_exit(NULL);} 