-
Notifications
You must be signed in to change notification settings - Fork 95
3.6 Shutdown
When it is imperative to shutdown the system quickly and deterministically, we do not want to drain()
since it may take a long time for all tasks to complete depending on the total amount of concurrent items. Instead calling terminate()
will signal all threads to stop and then will join on them. The interruption points for all threads are in between IO tasks or when performing a context switch from one coroutine to another. Thus, any worker thread will at most wait until the current task is finished or yields.
dispatcher.terminate(); //blocks for a very short period of time.
NOTE: Since terminate shuts down all threads, pending or unfinished tasks will never complete.
When the Dispatcher
object goes out of scope, its destructor will be called automatically, which in turn calls drain()
, followed immediately by terminate()
. This means that relying solely on the destructor to stop everything is also possible, however it may be slow.
One other way to ensure that terminate()
is called explicitly when a certain scope ends is to use a terminate guard.
//Scope starts here
{
ITerminate::Guard guard(dispatcher); //calls dispatcher.terminate() when scope ends
}
//All threads are stopped at this point