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

625 views Asked by At

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?

2

There are 2 answers

0
flashnik On BEST ANSWER

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.

4
qehgt On

sets appropriate flags for other threads to correctly finish their job

Usually it's not enough. You also must wait the threads to finish (thread.join(), or WaitForMultipleObjects, or something similar).