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

StackOverflow https://stackoverflow.com/questions/20333714

سؤال

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

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.

نصائح أخرى

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).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top