How to exit SW quickly
The fastest way to terminate a process is to call TerminateProcess()
. This simply stop the process from receiving any further time slices and releases all the resources allocated to it by windows.
This method of ending a process is not recommended though for various reasons. One of the for instance is that if you have an open file which you write to using the standard library, some data may still be buffered and not fully written to the file.
Efficient exit from multithreaded application (specifics)
In Windows, I use QueueUserAPC to call a function which throws an exception, causing threads to exit cleanly.
I wrote more about the details in this answer here:
How do I guarantee fast shutdown of my win32 app?
In summary, here's what happens:
Say thread A wants to terminate thread B (and then C, D, ...)
- Thread A calls
QueueUserAPC()
, passing the handle to thread B and the address of a function which will throw an Exception of class MyThreadExit. - Thread B runs normally until it calls something that checks for alertable waits. Maybe
WaitForSingleObjectEx
, maybeSleepEx
, or something else. - At this point, thread B runs the APC function passed earlier, causing the exception to be thrown in Thread B.
- All stack-allocated objects get automatically destructed correctly as the exception makes thread B 'unwind' its stack.
- The outermost thread function of thread B will catch the exception.
- Thread B now exits, possibly signalling to Thread A that it's done.
How do I shutdown, restart, or log off Windows via a bat file?
The most common ways to use the shutdown
command are:
shutdown -s
— Shuts down.shutdown -r
— Restarts.shutdown -l
— Logs off.shutdown -h
— Hibernates.Note: There is a common pitfall wherein users think
-h
means "help" (which it does for every other command-line program... exceptshutdown.exe
, where it means "hibernate"). They then runshutdown -h
and accidentally turn off their computers. Watch out for that.shutdown -i
— "Interactive mode". Instead of performing an action, it displays a GUI dialog.shutdown -a
— Aborts a previous shutdown command.
The commands above can be combined with these additional options:
-f
— Forces programs to exit. Prevents the shutdown process from getting stuck.-t <seconds>
— Sets the time until shutdown. Use-t 0
to shutdown immediately.-c <message>
— Adds a shutdown message. The message will end up in the Event Log.-y
— Forces a "yes" answer to all shutdown queries.Note: This option is not documented in any official documentation. It was discovered by these StackOverflow users.
I want to make sure some other really good answers are also mentioned along with this one. Here they are in no particular order.
- The
-f
option from JosephStyons - Using
rundll32
from VonC - The Run box from Dean
- Remote shutdown from Kip
Gently kill a process
Kernel event objects come to mind: your "manager" raises a named event. Your child processes should check the state of this event at least once in a while (or have a thread that continuously checks it).
Related Topics
Visual Studio 2013 Fatal Error C1041 /Fs
How to Tell If a Class/Struct Has No Data Members
Is There a Proper 'Ownership-In-A-Package' for 'Handles' Available
How to Pass a Boost Asio Tcp Socket to a Thread for Sending Heartbeat to Client or Server
How to Compare Two Standard Conversion Sequences Use the Rank of Contained Conversions
Boost::Asio Synchronous Client with Timeout
Why Isn't "0F" Treated as a Floating Point Literal in C++
Undefined Behavior in C/C++: I++ + ++I VS ++I + I++
How Are Exceptions Implemented Under the Hood
Passing Non-Pod Type to Variadic Function Is Undefined Behavior
Restrict Passed Parameter to a String Literal
Boost C++ Regex - How to Get Multiple Matches
Compiler Cannot Recognize My Class in C++ - Cyclic Dependency
How to Use 'Const_Cast' to Modify a Constant Variable
Vs: Unexpected Optimization Behavior with _Bitscanreverse64 Intrinsic
When Does an Incomplete Type Error Occur in C++
How to Expand Environment Variables in .Ini Files Using Boost