I have a ThreadPool class (simplified version here) in a multi-platform library which can be compiled either as static or dynamic. I create threads on first use, and clean them up in the destructor by submitting dummy jobs.
It works well except combination of Windows+DLL, where the threads are terminated by the OS before the class' destructor is invoked in library's DllMain function.
Is this a bug in Visual Studio? There was something similar already. If not, is there a more elegant solution than:
bool waitForThreads = true;
#if defined(WIN32) && !defined(ITK_STATIC)
DWORD dwWaitResult = WaitForSingleObject(m_Threads[0], 1);
if (dwWaitResult == WAIT_OBJECT_0) //thread has finished
{
//thread terminated by CRT
waitForThreads = false;
}
#endif
if (waitForThreads) //add dummy jobs for clean thread exit
{...}