I created a thread within a loaded dll by using std::thread. Then I unloaded the dll at main thread and try to reproduce the error that living thread running with unloaded codes. However, I notice the DLL cannot be unloaded after calling FreeLibrary
. I was using Visual Studio Modules View for knowing if the DLL is unloaded. The DLL is unloaded after the thread is finished.
I notice there is a similar ticket mentions that std::thread increase the reference count. I think it should make sense and I tried to read the implementation of the constructor of std::thread(MSVC version). I can see it uses _beginthreadex internally, but I cannot figure out which statement increase the DLL reference count. So my question is where is it?
If my theory above is wrong at the root, how the running thread prevent DLL from being unloaded?
By the way, I also tried creating thread by using CreateThread
, DLL can be unloaded and I can see the expected result (crash as my expected).
By the way again, how is it on Unix platforms?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…