C++11 std::mutex in Visual Studio 2012 deadlock when locked from DllMain()

前端 未结 2 653
攒了一身酷
攒了一身酷 2020-12-01 20:04

I am seeing a deadlock with std::mutex when the mutex is locked from DllMain() Below is a minimal DLL test case that exhibits the problem for me. M

相关标签:
2条回答
  • 2020-12-01 20:47

    It seems that using QueueUserAPC() to queue initialization is always executed before main() but out of the dreaded loader lock. This looks like a solution to my problem.

    EDIT 1

    After some testing it seems that the APC method works if I queue the APC from DllMain() but it does not work if I queue the APC from a ctor of a static global instance of a class. IOW, using the APC is not uniformly usable across all possible combinations of compilers and build modes for me.

    0 讨论(0)
  • 2020-12-01 20:48

    Check the Best Practices for Creating DLLs document:

    You should never perform the following tasks from within DllMain:

    • Call LoadLibrary or LoadLibraryEx (either directly or indirectly). This can cause a deadlock or a crash.
    • Synchronize with other threads. This can cause a deadlock.
    0 讨论(0)
提交回复
热议问题