![]() |
![]() |
| ||||||||
Cancellation PointsBe careful to cancel a thread only when cancellation is safe. The pthreads standard specifies several cancellation points, including:
Cancellation is enabled by default. At times you might want an application to disable cancellation. This has the result of deferring all cancellation requests until they are enabled again. See pthread_setcancelstate(3THR)for information about disabling cancellation. Cancel a Threadpthread_cancel(3THR)Use pthread_cancel(3THR) to cancel a thread.
How the cancellation request is treated depends on the state of the target thread. Two functions, pthread_setcancelstate(3THR) and pthread_setcanceltype(3THR), determine that state. Return Valuespthread_cancel() returns zero after completing successfully. Any other return value indicates that an error occurred. When the following condition occurs, the function fails and returns the corresponding value.
Enable or Disable Cancellationpthread_setcancelstate(3THR)Use pthread_setcancelstate(3THR) to enable or disable thread cancellation. When a thread is created, thread cancellation is enabled by default.
Return Valuespthread_setcancelstate() returns zero after completing successfully. Any other return value indicates that an error occurred. When the following condition occurs, the pthread_setcancelstate() function fails and returns the corresponding value.
Set Cancellation Typepthread_setcanceltype(3THR)Use pthread_setcanceltype(3THR) to set the cancellation type to either deferred or asynchronous mode. When a thread is created, the cancellation type is set to deferred mode by default. In deferred mode, the thread can be cancelled only at cancellation points. In asynchronous mode, a thread can be cancelled at any point during its execution. Using asynchronous mode is discouraged.
Return Valuespthread_setcanceltype() returns zero after completing successfully. Any other return value indicates that an error occurred. When the following condition occurs, the function fails and returns the corresponding value.
Create a Cancellation Pointpthread_testcancel(3THR)Use pthread_testcancel(3THR) to establish a cancellation point for a thread.
The pthread_testcancel() function is effective when thread cancellation is enabled and in deferred mode. Calling this function while cancellation is disabled has no effect. Be careful to insert pthread_testcancel() only in sequences where it is safe to cancel a thread. In addition to programmatically establishing cancellation points through the pthread_testcancel() call, the pthreads standard specifies several cancellation points. See Cancellation Pointsfor more details. There is no return value. | ||||||||
| ||||||||