MSVC 2013编译器会发生这种情况。我有一个包含2个项目的解决方案:.exe和.dll。
.exe源代码:
extern "C" int test_dll();
int main() {
test_dll();
return 0;
}
.dll源代码:
#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;
}
try_lock
来电永远不会回来。这是调用堆栈:
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]
如果在主.exe项目中创建了全局对象,则不会发生这种情况。只有在DLL中。这里发生了什么?
MSVC2015编译器(v140工具集)不会发生挂起。