Consider a simple program to evaluate a set of positions in a chess game.
Assume that there are k moves, each being evaluated by an independent thread.
If at any point of time, a position is established to be of a certain quality, the other positions that are known to be of worse quality must stop being evaluated.
In other words, the threads evaluating the corresponding board positions must be canceled.
Posix threads provide this cancellation feature.
A thread may cancel itself or cancel other threads.
pthread_cancel.
1 int
2 pthread_cancel (
3 pthread_t thread);
Here, thread is the handle to the thread to be canceled. When a call to this function is made, a cancellation is sent to the specified thread.
It is not guaranteed that the specified thread will receive or act on the cancellation. Threads can protect themselves against cancellation.
When a cancellation is actually performed, cleanup functions are invoked for reclaiming the thread data structures.
The pthread_cancel function returns after a cancellation has been sent. The cancellation may itself be performed later.