Hello guys
I am at the moment writing a multithreaded server (what kind of server, is not relevant) using the pthread library.
I want to encapsulate some of the c code into c++ classes and instead of using a ordinary function as I would usually use as the running thread function, I want to use a member function.
Here comes my problem. When you want to spawn new threads you usually just do something like this:
Code:
int thread_func(int *)
{
// thread code goes here
}
int main()
{
pthread_t thread
pthread_create(&thread, NULL, (void * (*) (void)) thread_func, (void *)0);
}
...and this compiles with no problems. The problem comes when i want to do the same within a class.
Code:
// class implementation
void* HandlerThread::thread_func(void *data)
{
// thread code goes here
}
void HandlerThread::start()
{
pthread_create(&thread, NULL, (void * (*) (void)) thread_func, (void *)0);
}
...this does not work. Here's the compiler output:
handler.cpp: In method `void HandlerThread::start()':
handler.cpp:51: no matches converting function `thread_func' to type `void * (*)(void *)'
handler.cpp:43: candidates are: void * HandlerThread::thread_func(void *)
The pthread_create function takes as its third parameter a pointer to a function that takes a void pointer and returns a void pointer.
There is problem casting to a pointer to function when I really got
a member function. Maybe this is not possible. If not, I hope someone have any suggestions
Hope you understood my problem