Why does this C++ static singleton never stop?

前端 未结 6 609
旧时难觅i
旧时难觅i 2020-12-28 08:48

i have implemented a singleton (static version) in C++. I know all the controversy about this pattern and potential thread-safety issues, but i am curious why this exact imp

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-28 09:26

    I've traced it down to void __cdecl _lock(int locknum) inside mlock.c. When main() ends, the main thread goes there and enters critical section EnterCriticalSection( _locktable[locknum].lock );. Then Singleton destructor gets called and the other thread tries to enter the same critical section, but can't, and so it starts waiting for main thread to leave the critical section. Main thread, in turn, waits for the other thread. So I guess it's a bug.

提交回复
热议问题