动态库卸载时同步停止用线程死锁

来源:互联网 发布:阿里云华北一地址 编辑:程序博客网 时间:2024/05/09 16:50

原因参照:


https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971(v=vs.85).aspx

Best Practices for Synchronization


Thread Synchronization in DllMain for DLL_THREAD_DETACH during DLL Unload
  • When the DLL is unloaded, the address space is not thrown away. Therefore, the DLL is expected to perform a clean shutdown. This includes thread synchronization, open handles, persistent state, and allocated resources.
  • Thread synchronization is tricky because waiting on threads to exit in DllMain can cause a deadlock. For example, DLL A holds the loader lock. It signals thread T to exit and waits for the thread to exit. Thread T exits and the loader tries to acquire the loader lock to call into DLL A’s DllMain with DLL_THREAD_DETACH. This causes a deadlock. To minimize the risk of a deadlock:
    • DLL A gets a DLL_THREAD_DETACH message in its DllMain and sets an event for thread T, signaling it to exit.
    • Thread T finishes its current task, brings itself to a consistent state, signals DLL A, and waits infinitely. Note that the consistency-checking routines should follow the same restrictions as DllMain to avoid deadlocking.
    • DLL A terminates T, knowing that it is in a consistent state.


0 0
原创粉丝点击