我不明白为什么这个简单的代码段有一个死锁:的std ::的Thread.join()的僵局
#include <atomic>
#include <thread>
#include <memory>
using namespace std;
class Test {
public:
Test() : mExit(false)
{
mThread = thread(bind(&Test::func, this));
}
~Test()
{
if (mThread.joinable())
{
mExit = true;
mThread.join();
}
}
private:
void func()
{
while (!mExit)
{
// do something
}
}
private:
atomic<bool> mExit;
thread mThread;
};
typedef unique_ptr<Test> TestPtr;
TestPtr gTest;
int main()
{
gTest = TestPtr(new Test);
return 0;
}
编辑 我打字错误的contstructor设置MEXIT =真
编辑2 我正在使用msvc2012和v110_xp工具集。
编辑3 问题消失,如果我明确地内主要
'mExit = false' ?? –
我输错了,抱歉。问题依然存在。 –
嗯。在你正确使用原子之前,这可能不会变好。并使用调试器来找出线程正在做什么。在杂草中关闭的可能性相当大,阻止了一些操作系统调用,而不是一次又一次地燃烧100%核心检查退出条件。 –