Search code examples
c++windowscommand-lineinterrupt-handlingatexit

Why are threads interrupted even when atexit or ConsoleHandler for SetConsoleCtrlhandler is executed?


I have a multithreaded application under Windows 7.

I need to correctly finish jobs in threads which have an open descriptors, connections and so on when a user presses 'X' in the corner of command line, 'Ctrl+C', shuts down OS and so on.

I've set a handler for SetConsoleHandler which sets appropriate flags for other threads to correctly finish their job. But all of them are interrupted and the y exit with code 0xc000013a. SOmetimes even my handler doesn't have time to set flag.

The same problem remains when I try to do the same operations in atexit handler.

Why are all threads stopped even during interruption handler? How can I avoid this and let all my threads finish their job?


Solution

  • The problem in my case was that some of child-children thread used timed-waiting on system resources so each of them needed to wake from waiting to join thread. And all of them were stopping consecutively so they required too much time to stop.