0

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?

flashnik
  • 1,900
  • 4
  • 19
  • 38

2 Answers2

0

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

qehgt
  • 2,972
  • 1
  • 22
  • 36
  • Yes, but all threads (including parent's) exit before join suceed. Joining child's threads is included in 'finishing job' in my opinion. – flashnik Dec 02 '13 at 17:38
  • @flashnik, How did you test it? Are you using console IO functions (like `gets`)? If so, try to check return value from such functions and `GetLastError` as well. For me it looks like your main thread exits too quickly, so your handler does not have a big chance to be executed. – qehgt Dec 02 '13 at 18:32
  • No, main thread is interrupted in joining threads. The handler thread is also interrupted. So it seems like something else just interrupts everybody :( – flashnik Dec 03 '13 at 10:12
  • @flashnik, that's weird. Because when I tried to reproduce this behavior, my demo app works fine. The handler had been called, it can wait for threads to finish etc. Please double check that your "main" thread does not exit too early (because if `main` exits then RTL forcibly terminates the process with all its threads). – qehgt Dec 03 '13 at 16:00
0

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.

flashnik
  • 1,900
  • 4
  • 19
  • 38