0

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?

yohjp
  • 2,985
  • 3
  • 30
  • 100
Kamran
  • 152
  • 2
  • 13

1 Answers1

0

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));
yohjp
  • 2,985
  • 3
  • 30
  • 100
  • `thunk` shall be static member function. _Conceptually_, you can treat member function `void* X::func(void* arg)` as normal function `void* func(X* this, void* arg)`. – yohjp Dec 18 '14 at 06:02