I want to stop the warning
server.cpp:823: warning: converting from 'void* (ClientHandler::)()' to 'void ()(void)'
in the call:
pthread_create(th, NULL,
(void* (*)(void*)) &ClientHandler::handle,
(void *) clientHandler);
where handle()
is a member function of ClientHandler
:
void* ClientHandler::handle();
I have difficulties deciphering the function-type message from the compiler.
The question is:
- Should I change the
handle()
interface? Can I get rid of casting overall? - Should I change the cast? To what exactly?
- Something completely different?
If you're looking to invoke the method
ClientHandler::handle
from a specific instance of theClientHandler
class, it's unfortunately a bit more complicated than your example as pointers to member functions are different than pointers to functions in general. See here for a complete description of what needs to be done to create pthreads in such a manner.You can't do that directly, pointers to member functions are not plain pointers to functions and can't be handed over to
C
callbacks directly.You'll need one level of indirection:
See the Pointers to members section of the C++FAQ for more information / alternatives.
For the validity of the cast in
callHandle
, see this question. You are sole responsible for making sure thathandle
is still alive and well whencallHandle
is called of course (and for the fact that it actually points to aClientHandle
).You need to pass a static cdecl function to
pthread_create
as given in this signature:The optional argument can be used to pass your
ClientHandler
object into the thread.