Reputation: 149
So I'm trying to use pthread_create but I'm getting
error: argument of type ‘void* (server::)(void*)’ does not match ‘void* (*)(void*)’
The class is defined as follows:
class server : public AL::ALModule
{
public:
server(boost::shared_ptr<AL::ALBroker> pBroker, const std::string& pName);
....
void *ThreadMain(void *arg);
}
};
And here's the function where I'm calling pthread from:
int server::listen() {
TCPServerSocket servSock(6004);
...
for (;;) {
clntSock = servSock.accept();
...
pthread_t threadID;
pthread_create(&threadID, NULL, this->ThreadMain,(void *) clntSock);
...
}
}
How do I cast ThreadMain from server:: to * ?
Thanks in advance!
Upvotes: 1
Views: 1242
Reputation: 1586
void *ThreadMain(void* arg)
should be
static void ThreadMain(void* arg)
and the invocation:
pthread_create(&threadID, NULL, &(server::ThreadMain), (void *) clntSock);
The thread you're creating has no way of knowing what your this
pointer is; C++ is trying to protect you from this fact by making you acknowledge that ThreadMain
is static and can't access non-static properties.
The problem with using &(this->anyFunc)
is that this
has no actual reference to its methods if they're not function pointers or virtual, nor does the method itself have any reference to a corresponding this
unless it's provided by the caller (implicitly).
Upvotes: 1