C++0x 线程初始化
c++0x thread initlization
可能的重复项:
使用成员函数启动线程
在 c++0x 中使用类方法定义线程构造函数时,如下所示,我得到函数无法解析。我做错了什么?
例如,如果我有
#include <thread>
using namespace std;
class A
{
public:
void doSomething();
A();
}
然后在 A 类的构造函数中,我想用 doSomething 启动一个线程。如果我像下面这样写,我会收到错误,指出 doSomething 未解决。我甚至>做某事。
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;
}
相关文章:
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 如何初始化大线程本地对象?
- 开始多线程代码之前的初始化
- 如何在堆上初始化线程?(使用 "new" 关键字)
- 线程局部变量的初始化顺序
- C++:用IIFE线程初始化静态局部变量安全吗
- 设置 JVM 通过 JNI 初始化时创建的线程数
- JNI 不满意链接错误: 动态链接库 (DLL) 初始化例程失败
- VC++6 线程安全静态初始化
- 初始化线程的正确方法
- 无法初始化线程矢量
- 如何使用win32从多线程上下文初始化线程基元
- 如何在c++中初始化线程局部变量
- 如何在 C++11 中的函数内初始化线程安全静态常量向量
- 全局静态初始化线程
- Visual Studio 2012中的std::atomic_flag静态初始化线程是否安全
- 我们如何在不杀死线程的情况下重新初始化线程
- 在VS2015中c++局部静态变量初始化线程安全吗?
- C++11:std::thread 在类中执行函数成员,并在构造函数中初始化线程
- 实例化线程而不运行它们