This is somewhat similiar to this : pthread function from a class
But the function that’s getting called in the end is referencing the this pointer, so it cannot be made static.
void * Server::processRequest()
{
std::string tmp_request, outRequest;
tmp_request = this->readData();
outRequest = this->parse(tmp_request);
this->writeReply(outRequest);
}
void * LaunchMemberFunction(void * obj)
{
return ((Server *)obj)->processRequest();
}
and then the pthread_create
Server SServer(soc);
pthread_create(&handler[tcount], &attr, (void*)LaunchMemberFunction,(void*)&SServer);
errors:
SS_Twitter.cpp:819: error: invalid conversion from void* to void* ()(void)
SS_Twitter.cpp:819: error: initializing argument 3 of int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)
You are casting the third argument to a
void* ((void*), and then getting an error, asvoid*cannot be cast to a function pointer.I believe it should compile if you just use
&LaunchMemberFunctioninstead.