I don't understand why this simple snippet has a dead lock:
#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;
}
Edit I typed wrong the contstructor set mExit = true
Edit 2 I'm using msvc2012 with v110_xp toolset.
Edit 3 The issue disappear if I explicity call gTest.release() inside main