This happens with the MSVC 2013 compiler. I have a solution with 2 projects: .exe and .dll.
The .exe source code:
extern "C" int test_dll();
int main() {
test_dll();
return 0;
}
The .dll source code:
#include <mutex>
struct AAAA {
AAAA() {
std::mutex mutex;
if (mutex.try_lock()) { // always blocks
mutex.unlock();
}
};
} globalObject;
extern "C" __declspec(dllexport)
int test_dll() {
return true;
}
The try_lock
call never returns. Here's the call stack:
msvcr120d.dll!Concurrency::details::ThreadScheduler::Create(const Concurrency::SchedulerPolicy & policy) Line 34
msvcr120d.dll!Concurrency::details::SchedulerBase::CreateWithoutInitializing(const Concurrency::SchedulerPolicy & policy) Line 285
msvcr120d.dll!Concurrency::details::SchedulerBase::GetDefaultScheduler() Line 654
msvcr120d.dll!Concurrency::details::SchedulerBase::CreateContextFromDefaultScheduler() Line 571
msvcr120d.dll!Concurrency::details::SchedulerBase::CurrentContext() Line 399
msvcr120d.dll!Concurrency::details::LockQueueNode::LockQueueNode(unsigned int timeout) Line 619
msvcr120d.dll!Concurrency::critical_section::try_lock() Line 1049
msvcp120d.dll!mtx_do_lock(_Mtx_internal_imp_t * * mtx, const xtime * target) Line 75
msvcp120d.dll!_Mtx_trylock(_Mtx_internal_imp_t * * mtx) Line 162
MutexDll.dll!std::_Mtx_trylockX(_Mtx_internal_imp_t * * _Mtx) Line 73
MutexDll.dll!std::_Mutex_base::try_lock() Line 46
MutexDll.dll!AAAA::AAAA() Line 8
MutexDll.dll!`dynamic initializer for 'AAA''() Line 21
[External Code]
This doesn't happen if the global object is created in the main .exe project. Only when in the DLL. What's going on here?
The hang doesn't occur with the MSVC2015 compiler (v140 toolset).