26

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?

James McNellis
  • 348,265
  • 75
  • 913
  • 977
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • Beta and Release Candidate issues? – Jaywalker Jun 06 '12 at 13:34
  • @Jaywalker Well I guess that's what I'm asking. Is it reasonable to expect `join()` to not hang if it's invoked after `main` has exited. If so, this is a bug which I'd like to get into MS Connect sooner rather than later. – Fraser Jun 06 '12 at 13:42
  • Could you try to test if the `std::thread::joinable()` still returns true before the hanging ? – Plexico Jun 06 '12 at 15:03
  • Please file a bug on [Microsoft Connect](http://connect.microsoft.com/VisualStudio) (I don't know if this is _actually_ a bug; I'm not that familiar with the `std::thread` specification. But it would be worth reporting nonetheless.) – James McNellis Jun 06 '12 at 16:48
  • @JamesMcNellis OK - done thanks. https://connect.microsoft.com/VisualStudio/feedback/details/747145 – Fraser Jun 06 '12 at 20:53
  • 1
    @Plexico `thread_.joinable()` does return `true`. – Fraser Jun 06 '12 at 20:54

5 Answers5

27

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 of ThreadTest, and is waiting (indefinitely) on the second thread to exit (via the call to join()).

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 the main() 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 in main() 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 the main() 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 calls join() and call that function at the end of main() - again avoiding the deadlock situation.

jwd
  • 10,837
  • 3
  • 43
  • 67
CWoods
  • 592
  • 6
  • 13
  • @CWoods - can you stick the link to my MS Connect bug report into your answer? I'd like to mark yours as correct and delete mine. – Fraser Nov 22 '12 at 12:31
  • @Frazer - Done! Glad to have helped! – CWoods Nov 26 '12 at 03:19
  • 1
    Unbelievable that this problem still exists and affects C++11 threads. I have seen similar behavior with a Windows port of pthreads 10 years ago... – Florian Winter Jan 11 '16 at 14:35
  • Yes, the bug still exists in MSVC++2013 for `_beginthreadex()` too: I've just refactored from C++11 `thread` to MSVC++ `_beginthreadex()`, no way. MSDN also says that `CreateThread` cannot be used for threads which use CRT (and I need `FILE*` operations): https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx . There is also no option to refactor to non-static variable, as the threading happens in a logging singleton. So MSVC++ 2013 is broken on threading, and CUDA Toolkit does not yet support MSVC++2015. %( – Serge Rogatch Aug 30 '16 at 11:32
  • @CWoods the MS link bug report is broken. Do you have an updated link? – TonySalimi Feb 27 '19 at 11:02
  • @hsalimi The landing page of the broken link suggests you search the C++ Developer Community Site (https://developercommunity.visualstudio.com/spaces/62/index.html) with the old ConnectID (747145) but as I type this it seems their search functionality is down unfortunately. You'll have to try it yourself later... – CWoods Mar 02 '19 at 06:14
  • The system is up again, but cannot find the updated link with the ID. – TonySalimi Mar 07 '19 at 15:17
  • @hsalimi I'm not having any luck either I'm afraid. The only thing that I can suggest is that you try reaching out directly to Microsoft (it's a long shot). As far as I can remember the issue was never actually fixed in any VS2012 or VS2013 version/patch. I don't think it was until VS2015 when the fix was actually released. – CWoods Mar 18 '19 at 12:49
  • @jwd Thanks for thinking of using the wayback machine to make the link working again! – CWoods Jan 29 '21 at 01:19
7

I realize this is an old question regarding VS2012, but the bug is still present in VS2013. For those who are stuck on VS2013, perhaps due to Microsoft's refusal to provide upgrade pricing for VS2015, I offer the following analysis and workaround.

The problem is that the mutex (at_thread_exit_mutex) used by _Cnd_do_broadcast_at_thread_exit() is either not yet initialized, or has already been destroyed, depending on the exact circumstances. In the former case, _Cnd_do_broadcast_at_thread_exit() tries to initialize the mutex during shutdown, causing a deadlock. In the latter case, where the mutex has already been destroyed via the atexit stack, the program will crash on the way out.

The solution I found is to explicitly call _Cnd_do_broadcast_at_thread_exit() (which thankfully is declared publicly) early during program startup. This has the effect of creating the mutex before anyone else tries to access it, as well as ensuring that the mutex continues to exist until the last possible moment.

So, to fix the problem, insert the following code at the bottom of a source module, for instance somewhere below main().

#pragma warning(disable:4073) // initializers put in library initialization area
#pragma init_seg(lib)

#if _MSC_VER < 1900
struct VS2013_threading_fix
{
    VS2013_threading_fix()
    {
        _Cnd_do_broadcast_at_thread_exit();
    }
} threading_fix;
#endif
Jim Barry
  • 71
  • 1
  • 3
1

I believe your threads have already been terminated and their resources freed following the termination of your main function and before static destruction. This is the behavior of the VC runtimes dating back to at least VC6.

Do child threads exit when the parent thread terminates

boost thread and process cleanup on windows

Community
  • 1
  • 1
  • This answer is specific to windows, but I assumed since you are using VS. – TractorPulledPork Jun 06 '12 at 13:39
  • I am using Windows, but I'm not sure your answer is right unfortunately. The main thread is the one which is executing `join`, so even though the function `main` has exited, the main thread has not terminated. Thanks anyway. – Fraser Jun 06 '12 at 13:47
  • More relevant content: http://stackoverflow.com/questions/8001989/boost-thread-and-process-cleanup-on-windows – TractorPulledPork Jun 06 '12 at 13:58
  • Perhaps the code that kills the threads is somewhere in the runtime on the outside of the main call but I can assure you your threads are gone by the time your destructors are called. – TractorPulledPork Jun 06 '12 at 14:03
  • 2
    I can pause the execution while the program is hanging and see the stacks for both the main thread trying to complete the `join` call, and the child thread in `_lock` of mlock.c, so the threads haven't terminated. Also, your answer doesn't really answer my question. Is this hanging a result of undefined behaviour or a bug in VS2012? – Fraser Jun 06 '12 at 14:30
0

My answer is too far late, but hope will help someone.

I was stucked by this bug, and i find a trick to solve this problem,it worked in my code.

int main()
{
    ThreadTest trick_obj;  //trick... You can put this line of code anywhere
    static ThreadTest std_test;
    return 1;
}
jayu
  • 1
-1

I have been battling this bug for a day, and found the following work-around, which turned out the be the least dirty trick:

Instead of returning, one can use the standard Windows API function call ExitThread() to terminate the thread. This method of course may mess up the internal state of the std::thread object and associated library, but since the program is going to terminate anyway, well, so be it.

#include <windows.h>

template<typename T>
class ThreadTest {
 public:
  ThreadTest() : thread_([] { SleepFor(10); ExitThread(NULL); }) {}
  ~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_;
};

The join() call apparently works correctly. However, I chose to use a more safe method in our solution. One can get the thread HANDLE via std::thread::native_handle(). With this handle we can call the Windows API directly to join the thread:

WaitForSingleObject(thread_.native_handle(), INFINITE);
CloseHandle(thread_.native_handle());

Thereafter, the std::thread object must not be destroyed, as the destructor would try to join the thread a second time. So we just leave the std::thread object dangling at program exit.

Timo Bingmann
  • 294
  • 1
  • 4