Possible Duplicate:
Start thread with member function
如下所示,在 c++0x 中使用类方法定义线程构造函数时,无法解析 i get 函数。我做错了什么?
例如,如果我有
#include <thread>
using namespace std;
class A
{
public:
void doSomething();
A();
}
然后在 A 类的构造函数中,我想用 doSomething 启动一个线程。如果我像下面这样写,我会收到 doSomething Unresolved 错误。我什至这个->做某事。
A::A()
{
thread t(doSomething);
}
试试这个:
class A
{
public:
void doSomething();
A()
{
thread t(&A::doSomething, this);
}
};
或
class A
{
public:
static void doSomething();
A()
{
thread t(&A::doSomething);
}
};
注意:你需要在某处加入你的线程,例如:
class A
{
public:
void doSomething()
{
std::cout << "output from doSomething" << std::endl;
}
A(): t(&A::doSomething, this)
{
}
~A()
{
if(t.joinable())
{
t.join();
}
}
private:
std::thread t;
};
int main()
{
A a;
return 0;
}