C++ c++;0x线程初始化
可能重复:C++ c++;0x线程初始化,c++,multithreading,c++11,C++,Multithreading,C++11,可能重复: 如下图所示,在c++0x中使用类方法定义线程构造函数时,无法解析i get函数。我做错了什么 例如,如果我有 #include <thread> using namespace std; class A { public: void doSomething(); A(); } 试试这个: class A { public: void doSomething(); A() { thread t(&A::doS
如下图所示,在c++0x中使用类方法定义线程构造函数时,无法解析i get函数。我做错了什么 例如,如果我有
#include <thread>
using namespace std;
class A
{
public:
void doSomething();
A();
}
试试这个:
class A
{
public:
void doSomething();
A()
{
thread t(&A::doSomething, this);
}
};
或
注意:您需要在某个地方加入您的线程,例如:
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;
}
A类
{
公众:
无效剂量测定法()
{
std::cout试试这个:
class A
{
public:
void doSomething();
A()
{
thread t(&A::doSomething, this);
}
};
或
注意:您需要在某个地方加入您的线程,例如:
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;
}
A类
{
公众:
无效剂量测定法()
{
std::库蒂,这起作用了。如果我不通过“这个”会不会不起作用?除非soSomething是静态成员函数great!!这是响应。@Jimm,它接受函数,然后是要传递的参数列表。该函数需要一个实例。如果是静态的,为什么&a::doSomething而不是a::doSomething?Ty不起作用。如果我不传递“this”,它会不起作用吗?除非soSomething是静态成员函数great!!这是响应。@Jimm,它接受函数,然后是要传递的参数列表。该函数需要一个实例。如果是静态的,为什么不是&a::doSomething而不是a::doSomething?