The problem is that pthread_create
is a C function, not a C++ one, so using it from C++ can be tricky. The third argument needs to be a function pointer, but you're trying to call it with a method pointer which is not the same thing.
C++ does allow implicitly converting a static method pointer into a function pointer, so you can do what you want with that (that's why you get an error saying 'task' isn't static -- because if it was, it could be converted to a function pointer and used). Usually you then use the 4th argument to hold "this" so you can then call a non-static method from the static method
class Foo
{
void assingTask()
{
pthread_t myThread;
int i;
pthread_create(&myThread, NULL, start_task, this)
}
static void *start_task(void *this_) {
return static_cast<Foo *>(this_)->task();
}
void * task()
{
//DO Stuff
}
};
The above code has the problem that you "lose" the pthread_t handle (don't store it anywhere accessable), so you can't join or detach from the thread, but that could be fixed many ways.