Why does this C++ static singleton never stop?

前端 未结 6 610
旧时难觅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条回答
  •  情书的邮戳
    2020-12-28 09:30

    On the main thread, after main() terminates, the CRT acquires the exit lock and calls your static instance destructor, which waits for your background thread to exit.

    On the background thread, after your thread function terminates, the CRT attempts to acquire the exit lock to do some thread termination work. This blocks forever because the exit lock is held by the main thread, which is waiting for this thread to exit.

    It's a simple deadlock that's caused by the CRT implementation. The bottom line is that you can't await thread termination in a static instance destructor on Windows.

提交回复
热议问题