std::thread cause deadlock in DLLMain

前端 未结 4 980
无人共我
无人共我 2020-12-28 11:14

So, this is what I\'m talking about: std is complex.

In VS2013 this simple program will cause a deadlock.

#include 
#include 

        
4条回答
  •  无人及你
    2020-12-28 11:23

    You're mixing the platform level with std level. Calling the raw winapi function CreateThread can work in DllMain. But there's no guarantee about how std::thread will interact with the platform. It's well known that it's extremely dangerous to be doing things like this in DllMain, so I don't recommend it at all. If you insist on trying, then you're going to need to tip-toe around and call the winapi directly, avoiding consequences of the std implementation.

    As for "why", it shouldn't really matter, but after a very quick look in the debugger, it seems that the MSVC implementation has a handshake with the new thread for exchanging arguments and resources. So it requires synchronization to know when the resources have been handed off. Seems reasonable.

提交回复
热议问题