我正在尝试在类的构造函数中创建线程,该类将在该类中运行几个函数,我试过这个:
ServerLogic::ServerLogic(SOCKET sock)
{
this->sock = sock;
this->dispatchThread = new std::thread(this->dispatchMessage);
}
void ServerLogic::dispatchMessage(){
/*
* this function will handle the connetions with the clients
*/
char recievedMsg[1024];
int connectResult;
//receive data
while (true){
connectResult = recv(this->sock, recievedMsg, sizeof(recievedMsg), 0);
//in case everything good send to diagnose
if (connectResult != SOCKET_ERROR){
this->messagesToDiagnose.push(std::string(recievedMsg));
}
else{
/*
* destructor
*/
}
}
}
但它给了我一个错误: 'ServerLogic :: dispatchMessage':函数调用缺少参数列表;使用'& ServerLogic :: dispatchMessage'创建指向成员的指针。
IntelliSense:函数“std :: thread :: thread(const std :: thread&)”(在“C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ VC \ include \ thread”第70行声明“)无法引用 - 它是一个已删除的函数。
答案 0 :(得分:3)
我认为错误信息基本上是在告诉你该怎么做。考虑以下代码(这是有问题的,但只是用来说明这一点):
load
要指定成员函数,请使用
#include <thread>
class foo
{
public:
foo()
{
std::thread(&foo::bar, this);
}
void bar()
{
}
};
int main()
{
foo f;
}