The following example runs successfully (i.e. doesn't hang) if compiled using Clang 3.2 or GCC 4.7 on Ubuntu 12.04, but hangs if I compile using VS11 Beta or VS2012 RC.
#include <iostream>
#include <string>
#include <thread>
#include "boost/thread/thread.hpp"
void SleepFor(int ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
template<typename T>
class ThreadTest {
public:
ThreadTest() : thread_([] { SleepFor(10); }) {}
~ThreadTest() {
std::cout << "About to join\t" << id() << '\n';
thread_.join();
std::cout << "Joined\t\t" << id() << '\n';
}
private:
std::string id() const { return typeid(decltype(thread_)).name(); }
T thread_;
};
int main() {
static ThreadTest<std::thread> std_test;
static ThreadTest<boost::thread> boost_test;
// SleepFor(100);
}
The issue appears to be that std::thread::join()
never returns if it is invoked after main
has exited. It is blocked at WaitForSingleObject
in _Thrd_join
defined in cthread.c.
Uncommenting SleepFor(100);
at the end of main
allows the program to exit properly, as does making std_test
non-static. Using boost::thread
also avoids the issue.
So I'd like to know if I'm invoking undefined behaviour here (seems unlikely to me), or if I should be filing a bug against VS2012?
Tracing through Fraser's sample code in his connect bug (https://connect.microsoft.com/VisualStudio/feedback/details/747145) with VS2012 RTM seems to show a fairly straightforward case of deadlocking. This likely isn't specific to
std::thread
- likely_beginthreadex
suffers the same fate.What I see in the debugger is the following:
On the main thread, the
main()
function has completed, the process cleanup code has acquired a critical section called_EXIT_LOCK1
, called the destructor ofThreadTest
, and is waiting (indefinitely) on the second thread to exit (via the call tojoin()
).The second thread's anonymous function completed and is in the thread cleanup code waiting to acquire the
_EXIT_LOCK1
critical section. Unfortunately, due to the timing of things (whereby the second thread's anonymous function's lifetime exceeds that of themain()
function) the main thread already owns that critical section.DEADLOCK.
Anything that extends the lifetime of
main()
such that the second thread can acquire_EXIT_LOCK1
before the main thread avoids the deadlock situation. That's why the uncommenting the sleep inmain()
results in a clean shutdown.Alternatively if you remove the static keyword from the
ThreadTest
local variable, the destructor call is moved up to the end of themain()
function (instead of in the process cleanup code) which then blocks until the second thread has exited - avoiding the deadlock situation.Or you could add a function to
ThreadTest
that callsjoin()
and call that function at the end ofmain()
- again avoiding the deadlock situation.