简体   繁体   中英

Argument type mismatch (void*)

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!

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM