简体   繁体   English

参数类型不匹配(无效*)

[英]Argument type mismatch (void*)

So I'm trying to use pthread_create but I'm getting 所以我正在尝试使用pthread_create但我正在

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: 这是我从中调用pthread的函数:

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 * ? 如何将ThreadMain从server ::强制转换为*?

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; 您正在创建的线程无法知道this指针是什么。 C++ is trying to protect you from this fact by making you acknowledge that ThreadMain is static and can't access non-static properties. C ++试图通过使您承认ThreadMain是静态的并且不能访问非静态属性来保护您免受这一事实的侵害。

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). 与使用问题&(this->anyFunc)this无实际参照其方法,如果它们不是函数指针或虚拟的,也不该方法本身具有到对应的任何参考this ,除非它是由调用者提供(隐含的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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