title: Exit thread upon deleting static object during unload DLL causes deadlock?
body: <p>I have one instance (global/static object) ClassA inside of my delay-loaded DLL. This object inside has an "Observer" thread which is required to perform graceful shutdown. When I call FreeLibrary I noticed that during deletion of this static object my thread requested to shutdown but hangs on _endthreadex() and causes deadlock. It doesn't matter if I call _endthreadex explicitly or implicitly. It doesn't matter if object is global or static - same result.
  This thread wrapped in ClassB (implemented by template with custom message loop). There is a request to shutdown thread (post message) and following WaitForSingleObject which never returns for given thread heandle.</p>
  
  <p>Same "template thread class" used everywhere in the code and shutdown works great. The only issue when deleting static obj. I think there is some lock inside of _endthreadex() which is already locked upon dll unload and deleting of static objects.</p>
  
  <p>Thread started with _beginthreadex.
  ps. When I instantiated same static obj inside of App - app closes without any significant issues.</p>
  
  <p>Any ideas why _endtreadex causes deadlock? How to avoid it?</p>