Thu*_*orm 0 c++ multithreading stl c++11
每次调用ObjParser :: loadData()时我都会尝试启动一个新线程,就像他们在这个例子中所做的那样.
所以我写了这段代码.
#include <thread>
void ObjParser::loadData()
{
thread loadingThread(_loadData);
loadingThread.detach();
}
void ObjParser::_loadData()
{
//some code
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译它时,我收到此错误:
error C3867: 'ObjParser::_loadData': function call missing argument list; use '&ObjParser::_loadData' to create a pointer to member
Run Code Online (Sandbox Code Playgroud)
所以我创建了一个指向成员函数的指针:
#include <thread>
void ObjParser::loadData()
{
thread loadingThread(&ObjParser::_loadData);
loadingThread.detach();
}
void ObjParser::_loadData()
{
//some code
}
Run Code Online (Sandbox Code Playgroud)
但是编译器抱怨说:
error C2064: term does not evaluate to a function taking 0 arguments
Run Code Online (Sandbox Code Playgroud)
我不知道导致问题的原因,请你给我一个如何解决这个问题的提示.
_loadData
似乎是一个非静态成员,所以你需要在一个对象上调用它 - 可能是loadData
被调用的同一个对象:
thread loadingThread(&ObjParser::_loadData, this);
Run Code Online (Sandbox Code Playgroud)
或者是一个lambda
thread loadingThread([this]{_loadData();});
Run Code Online (Sandbox Code Playgroud)
或者,我可能会删除额外的函数,只使用lambda:
thread loadingThread([this]{ // or [] if you don't need to access class members
// some code
});
Run Code Online (Sandbox Code Playgroud)