How do I guarantee fast shutdown of my win32 app?

前端 未结 11 1805
夕颜
夕颜 2020-12-03 19:13

I\'ve got a C++ Win32 application that has a number of threads that might be busy doing IO (HTTP calls, etc) when the user wants to shutdown the application. Currently, I p

相关标签:
11条回答
  • 2020-12-03 19:24

    Use overlapped IO so that you're always in control of the threads that are dealing with your I/O and can always stop them at any point; you either have them waiting on an IOCP and can post an application level shutdown code to it, OR you can wait on the event in your OVERLAPPED structure AND wait on your 'all threads please shutdown now' event as well.

    In summary, avoid blocking calls that you can't cancel.

    If you can't and you're stuck in a blocking socket call doing IO then you could always just close the socket from the thread that has decided that it's time to shut down and have the thread that's doing IO always check the 'shutdown now' event before retrying...

    0 讨论(0)
  • 2020-12-03 19:26

    If you want to pull the plug messily, exit(0) will do the trick.

    0 讨论(0)
  • 2020-12-03 19:28

    I'd recommend having your GUI and work be done on different threads. When a user requests a shutdown, dismiss the GUI immediately giving the appearance that the application has closed. Allow the worker threads to close gracefully in the background.

    0 讨论(0)
  • 2020-12-03 19:29

    If you need to shutdown suddenly: Just call ExitProcess - which is what is going to be called just as soon as you return from WinMain anyway. Windows itself creates many worker threads that have no way to be cleaned up - they are terminated by process shutdown.

    If you have any threads that are performing writes of some kind - obviously those need a chance to close their resources. But anything else - ignore the bounds checker warnings and just pull the rug from under their feet.

    0 讨论(0)
  • 2020-12-03 19:34

    I use an exception-based technique that's worked pretty well for me in a number of Win32 applications.

    To terminate a thread, I use QueueUserAPC() to queue a call to a function which throws an exception. However, the exception that's thrown isn't derived from the type "Exception", so will only be caught by my thread's wrapper procedure.

    The advantages of this are as follows:

    • No special code needed in your thread to make it 'stoppable' - as soon as it enters an alertable wait state, it will run the APC function.
    • All destructors get invoked as the exception runs up the stack, so your thread exits cleanly.

    The things you need to watch for:

    • Anything doing catch (...) will eat your exception. User code should always use catch(const Exception &e) or similar!
    • Make sure your I/O and delays are done in an "alertable" way. For example, this means calling sleepex(N, true) instead of sleep(N).
    • CPU-bound threads need to call sleepex(0,true) occasionally to check for termination.

    You can also 'protect' areas of your code to prevent task termination during critical sections.

    0 讨论(0)
  • 2020-12-03 19:34

    Best way: Do your work while the app is running, and do nothing (or as close to) at shutdown (works for startup too). If you stick to that pattern, then you can tear down the threads immediately (rather than "being nice" about it) when the shutdown request comes without worrying about work that still needs to be done.

    In your specific situation, you'd probably need to wait for IO to finish (writes, at least) if you're doing local work there. HTTP requests and such you can probably just abandon/close outright (again, unless you're writing something). But if it is the case that you're writing during this shutdown and waiting on that, then you may want to notify the user of that, rather than letting your process look hung while you're wrapping things up.

    0 讨论(0)
提交回复
热议问题