Safe Thread Cancellation
The preceding example and discussion show how we can safely cancel a target thread that uses alertable wait states. Such cancellation is sometimes called synchronous cancellation, despite the use of APCs, because the cancellation, which is caused by the boss's QueueUserAPC call, can only take effect when the target thread reaches a safe alertable wait state.Synchronous cancellation requires the target thread to cooperate and allow itself to be canceled from time to time. Event waits are a natural place to enter an alertable wait state because, as a system shuts down, the event may never be signaled again. Mutex waits could also be alertable to allow thread waiting on a resource that may not become available again. For example, a boss thread could break deadlocks with this technique.Asynchronous thread cancellation is useful if it is necessary to signal a compute-bound thread that seldom, if ever, waits for I/O or events. Windows does not allow asynchronous cancellation, although there are techniques, using processor-specific code, to interrupt a specified thread.
