Implementing pthreads in .h and .cpp

88 views Asked by At

The normal way to pass function as arguments in pthreads for pthread_create method is

pthread_create(&thread,NULL,func,(void*)arg)

while func() is declared/defined as

void* func(void* arg);

but whenever I want to call pthread_create it in separate .cpp in visual studio 2012 it gives following error, as shown in the pic

The error in visual studio

but the error goes away if I define the function static.

static void* func(void* arg);

Any suggestions how to pass it without error without making it static?

1

There are 1 answers

1
yohjp On

The error message says that AppendData_Linux is member function of XMLParse class, and that can't convert to pointer to normal (non-member) function required by pthread_create.

Here is solution:

class X {
   void* arg;
public:
   void* func() { ... }

   static void* thunk(void* self) {
     return reinterpret_cast<X*>(self)->func();
   }
};

X obj;
pthread_create(thread, NULL, &X::thunk, reinterpret_cast<void*>(&obj));