title: Visual Studio 2013 std::mutex and the dreaded Windows loader lock
body: <p>Some time ago I have encountered a problem during application startup that I could not lock mutexes from below <code>DllMain()</code>: <a href="https://stackoverflow.com/questions/14711263/c11-stdmutex-in-visual-studio-2012-deadlock-when-locked-from-dllmain">C++11 std::mutex in Visual Studio 2012 deadlock when locked from DllMain()</a>. I have worked around as mentioned in the accepted answer.</p>
  
  <p>Now I am having a similar issue but this time caused by process shutdown:</p>
  
  <pre><code>KernelBase.dll!RaiseException() Unknown
  msvcr120d.dll!_CxxThrowException(void * pExceptionObject, const _s__ThrowInfo * pThrowInfo) Line 154    C++
  msvcr120d.dll!Concurrency::details::SchedulerBase::SchedulerBase(const Concurrency::SchedulerPolicy &amp; policy) Line 155  C++
  msvcr120d.dll!Concurrency::details::ThreadScheduler::ThreadScheduler(const Concurrency::SchedulerPolicy &amp; policy) Line 26   C++
  msvcr120d.dll!Concurrency::details::ThreadScheduler::Create(const Concurrency::SchedulerPolicy &amp; policy) Line 34    C++
  msvcr120d.dll!Concurrency::details::SchedulerBase::CreateWithoutInitializing(const Concurrency::SchedulerPolicy &amp; policy) Line 285  C++
  msvcr120d.dll!Concurrency::details::SchedulerBase::GetDefaultScheduler() Line 654   C++
  msvcr120d.dll!Concurrency::details::SchedulerBase::CreateContextFromDefaultScheduler() Line 571 C++
  msvcr120d.dll!Concurrency::details::SchedulerBase::CurrentContext() Line 399    C++
  msvcr120d.dll!Concurrency::details::LockQueueNode::LockQueueNode(unsigned int timeout) Line 619 C++
  msvcr120d.dll!Concurrency::critical_section::lock() Line 1026   C++
  msvcp120d.dll!mtx_do_lock(_Mtx_internal_imp_t * * mtx, const xtime * target) Line 67    C++
  msvcp120d.dll!_Mtx_lock(_Mtx_internal_imp_t * * mtx) Line 154   C++
  log4cplusUD.dll!std::_Mtx_lockX(_Mtx_internal_imp_t * * _Mtx) Line 68   C++
  log4cplusUD.dll!std::_Mutex_base::lock() Line 42    C++
  log4cplusUD.dll!log4cplus::thread::Mutex::lock() Line 80    C++
  log4cplusUD.dll!log4cplus::thread::SyncGuard&lt;log4cplus::thread::Mutex&gt;::SyncGuard&lt;log4cplus::thread::Mutex&gt;(const log4cplus::thread::Mutex &amp; m) Line 208    C++
  log4cplusUD.dll!log4cplus::spi::ObjectRegistryBase::clear() Line 113    C++
  log4cplusUD.dll!log4cplus::spi::FactoryRegistry&lt;log4cplus::spi::LocaleFactory&gt;::~FactoryRegistry&lt;log4cplus::spi::LocaleFactory&gt;() Line 156  C++
  log4cplusUD.dll!log4cplus::`anonymous namespace'::DefaultContext::~DefaultContext() C++
  log4cplusUD.dll!log4cplus::`anonymous namespace'::DefaultContext::`scalar deleting destructor'(unsigned int)    C++
  log4cplusUD.dll!log4cplus::`anonymous namespace'::destroy_default_context() Line 90 C++
  log4cplusUD.dll!log4cplus::thread_callback(void * __formal, unsigned long fdwReason, void * __formal) Line 457  C++
  log4cplusUD.dll!DllMain(HINSTANCE__ * hinstDLL, unsigned long fdwReason, void * lpReserved) Line 480    C++
  log4cplusUD.dll!__DllMainCRTStartup(void * hDllHandle, unsigned long dwReason, void * lpreserved) Line 508  C
  log4cplusUD.dll!_DllMainCRTStartup(void * hDllHandle, unsigned long dwReason, void * lpreserved) Line 473   C
  ntdll.dll!LdrShutdownProcess()  Unknown
  ntdll.dll!RtlExitUserProcess()  Unknown
  msvcr120d.dll!__crtExitProcess(int status) Line 776 C
  msvcr120d.dll!doexit(int code, int quick, int retcaller) Line 679   C
  msvcr120d.dll!exit(int code) Line 426   C
  timeformat_test.exe!__tmainCRTStartup() Line 662    C
  timeformat_test.exe!mainCRTStartup() Line 466   C
  kernel32.dll!BaseThreadInitThunk()  Unknown
  ntdll.dll!RtlUserThreadStart()  Unknown
  </code></pre>
  
  <p>The exception that it is raising is <a href="http://msdn.microsoft.com/en-us/library/dd470882.aspx" rel="nofollow noreferrer"><code>scheduler_resource_allocation_error</code></a>. It is caused by failure of <code>RegisterWaitForSingleObject()</code> call.</p>
  
  <p>The previous workaround will not help. I would have to be able to globally disable locking for all mutexes in various classes.</p>
  
  <p>How the hell am I supposed to use C++11 threading and synchronization facilities if the Visual Studio 2013 does not let me clean up at process exit?</p>