Hello!
I have been trying to create multiple threads in a C++ program I am developing. Moreover, I plan to create a nice class to enclose pthreads. However, when trying to pass a member function (virtual, non-virtual) to pthread_create I always get this error:
mtkWindow.cc: In member function `void* mtkWindow::run()':
mtkWindow.cc:93: error: argument of type `void*(mtkWindow::)(void*)' does not
match `void*(*)(void*)'
I was reading about templates to solve the problem, but I think I am still stuck with it when I get down to pthread_create. I have also tried casting it to anything I could think of with no results ... Thanks!
Hello,
I think I found a solution: instead of directly passing the member function to pthread_create, pass a extern "C" declared function to it with the this pointer as an argument. Then, in the extern "C" function call this->thread_main(). Thanks! (Sorry for posting a dud topic... )
The difference is that you're passing a member function pointer instead of a function pointer. The first requires the implicit "this" pointer, the second forbids it. To call the first requires an object to call it on. You can also make a static member function (that doesn't use a this pointer) instead. Your function doesn't even have to be extern "C", but just can't be in a class.
Candy wrote:The difference is that you're passing a member function pointer instead of a function pointer. The first requires the implicit "this" pointer, the second forbids it. To call the first requires an object to call it on. You can also make a static member function (that doesn't use a this pointer) instead. Your function doesn't even have to be extern "C", but just can't be in a class.