int pthread_cancel(pthread_t thread);
Thread cancellation lets a thread terminate the execution of any other thread in the process. When a cancellation requested is acted on, the target thread (the thread being cancelled) is allowed to defer cancellation requests and to perform application-specific cleanup processing.
As a thread acquires resources (for example, mutexes) around areas where it may be cancelled (that is, before a cancellation point), it needs to push cancellation cleanup handlers along with the acquisition of these resources. The cleanup handlers release these resources and are invoked only if the thread were to be cancelled. As the thread leaves the last cancellation point before releasing a resource, it needs to pop the cleanup handler it had pushed earlier for this resource.
When a thread is cancelled, all
the currently stacked cleanup handlers are executed and thread
execution is terminated when the last cancellation cleanup
handler returns. The thread exit status returned by
The thread's cancellation state and type determine when, if ever, a thread cancellation request is acted on. The cancellation state can be set to PTHREAD_CANCEL_DISABLE, in which case all cancellation requests are held pending. If the cancellation state is PTHREAD_CANCEL_ENABLE, cancellation requests are acted on according to the threads cancellation type. The default cancellation state is PTHREAD_CANCEL_ENABLE.
The thread cancellation type determines when the cancellation request is acted on. If the cancellation type is set to PTHREAD_CANCEL_ASYNCHRONOUS, the cancellation request is acted on immediately. If the cancellation type is set to PTHREAD_CANCEL_DEFERRED, the cancellation request is not acted on until the thread reaches a cancellation point. (see Cancellation Points). The default cancellation type is PTHREAD_CANCEL_DEFERRED.
If cancellation is enabled and
asynchronous cancellation has been requested, the thread must
call only async-cancel-safe functions, since the thread may be
cancelled by the system at any time. At this time, the only async-cancel-safe
If cancellation is enabled, and its type is deferred, the system tests for pending cancellation requests in certain defined functions (usually any blocking system function). These points are known as cancellation points.
Cancellation points may be
created in user code with the
Other thread-related functions that are cancellation points include
The following functions are cancellation points:
In addition, a cancellation point may occur in the following functions:
An application should set up a cancellation cleanup handling function to restore any resources before a thread reaches a cancellation point. Specified cancellation points allow programmers to easily keep track of actions needed in a cancellation cleanup handler. A thread should only be made asynchronously cancelable when it is not in the process of acquiring or releasing resources (or locks), or otherwise, not in a difficult or impossible recover state.
Cancellation handlers are installed and removed with the
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
pthread_cleanup_pop(), pthread_cleanup_push(), pthread_join(), pthread_key_create(), pthread_setcancelstate(), pthread_setcanceltype(), pthread_testcancel()
PTC MKS Toolkit 10.0 Documentation Build 6.