C++ 中构造函数中的线程
Threads in Constructor in C++
我正在尝试在一个类的构造函数中创建线程,该函数将在该类中运行几个函数,我已经尝试过:
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::d ispatchMessage':函数调用缺少参数列表;使用"&ServerLogic::d ispatchMessage"创建指向成员的指针。
IntelliSense:函数"std::thread::thread(const std::thread &)"(在"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\thread")的第70行声明)无法引用 - 它是一个已删除的函数。
我认为错误消息基本上是在告诉您该怎么做。 考虑以下代码(这是有问题的,但只是为了说明这一点):
#include <thread>
class foo
{
public:
foo()
{
std::thread(&foo::bar, this);
}
void bar()
{
}
};
int main()
{
foo f;
}
若要指定成员函数,请使用
std::thread(&foo::bar, this);
相关文章:
- 如何将元素添加到数组的线程安全函数?
- 为什么std::async使用同一个线程运行函数
- 非静态成员失败的线程调用函数
- 线程构造函数周围的可变参数模板包装器无法编译
- 通过std::shared_ptr使用Rcpp和RcppParallel的线程安全函数指针
- 如何解决传递给boost线程的函数中的歧义
- 返回可中断线程的函数
- 包装标准::线程调用函数
- 为什么Win API线程执行函数,而标准线程则没有
- C 好奇的情况将TypeName/DataType名称/类名称传递给线程构造函数
- 线程对象函数 C++
- 为线程构造函数传递引用以将其绑定到函数失败
- C++多线程在函数调用期间避免交错
- 为什么c++中线程构造函数和绑定的函数签名是相同的
- 了解线程构造函数的基础知识
- 线程构造函数如何检测右值引用
- std::线程构造函数(变量的数量)
- C++线程构造函数,使用new(object)会造成内存泄漏
- 如何将类方法传递给另一个函数,就像线程构造函数中发生的那样
- std::线程构造函数中的可移动参数(Visual c++ 2012)