Please look at code snippet below // these code blocks defined under single .cpp unit in DLL project
static std::thread sendMessageThread; // define as local static to cpp
bool callFunction()
{
/// Some simple code without spawning any thread or fibers
}
void WINAPI OnEngineStart()
{
sendMessageThread = std::thread(callFunction);
};
BOOL WINAPI DllMain(HINSTANCE hDll, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
if (sendMessageThread.joinable())
{
sendMessageThread.join(); // is this safe?
}
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
};
If the process which loaded the DLL call OnEngineStart function then whenever it unloads the DLL(using FreeLibrary WinAPI function), will it wait until sendMessageThread gets finished? and continue with DLL unload procedure?
std::thread::join()
function? Should I set semaphore or event under DLL_THREAD_DETACH and upon that signal stop the running thread ? ... If thread stopped before the DLL_PROCESS_DETACH invoke join function safe right? – Sep 26 '17 at 15:16