PTHREAD_TESTCANCEL(3) Library Functions Manual PTHREAD_TESTCANCEL(3)

pthread_setcancelstate, pthread_setcanceltype, pthread_testcancelset cancelability state

#include <pthread.h>

int
pthread_setcancelstate(int state, int *oldstate);

int
pthread_setcanceltype(int type, int *oldtype);

void
pthread_testcancel(void);

The () function atomically both sets the calling thread's cancelability state to the indicated state and, if oldstate is not NULL, returns the previous cancelability state at the location referenced by oldstate. Legal values for state are PTHREAD_CANCEL_ENABLE and PTHREAD_CANCEL_DISABLE.

The () function atomically both sets the calling thread's cancelability type to the indicated type and, if oldtype is not NULL, returns the previous cancelability type at the location referenced by oldtype. Legal values for type are PTHREAD_CANCEL_DEFERRED and PTHREAD_CANCEL_ASYNCHRONOUS.

The cancelability state and type of any newly created threads, including the thread in which () was first invoked, are PTHREAD_CANCEL_ENABLE and PTHREAD_CANCEL_DEFERRED respectively.

The () function creates a cancellation point in the calling thread. The pthread_testcancel() function has no effect if cancelability is disabled.

The cancelability state of a thread determines the action taken upon receipt of a cancellation request. The thread may control cancellation in a number of ways.

Each thread maintains its own “cancelability state” which may be encoded in two bits:

When cancelability is PTHREAD_CANCEL_DISABLE, cancellation requests against the target thread are held pending.
When cancelability is enabled and the cancelability type is PTHREAD_CANCEL_ASYNCHRONOUS, new or pending cancellation requests may be acted upon at any time. When cancelability is enabled and the cancelability type is PTHREAD_CANCEL_DEFERRED, cancellation requests are held pending until a cancellation point (see below) is reached. If cancelability is disabled, the setting of the cancelability type has no immediate effect as all cancellation requests are held pending; however, once cancelability is enabled again the new type will be in effect.

Cancellation points will occur when a thread is executing the following functions: (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), pthread_testcancel(), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ().

If successful, the pthread_setcancelstate() and pthread_setcanceltype() functions will return zero. Otherwise, an error number shall be returned to indicate the error.

The pthread_setcancelstate() and pthread_setcanceltype() functions are used to control the points at which a thread may be asynchronously canceled. For cancellation control to be usable in modular fashion, some rules must be followed.

For purposes of this discussion, consider an object to be a generalization of a procedure. It is a set of procedures and global variables written as a unit and called by clients not known by the object. Objects may depend on other objects.

First, cancelability should only be disabled on entry to an object, never explicitly enabled. On exit from an object, the cancelability state should always be restored to its value on entry to the object.

This follows from a modularity argument: if the client of an object (or the client of an object that uses that object) has disabled cancelability, it is because the client does not want to have to worry about how to clean up if the thread is canceled while executing some sequence of actions. If an object is called in such a state and it enables cancelability and a cancellation request is pending for that thread, then the thread will be canceled, contrary to the wish of the client that disabled.

Second, the cancelability type may be explicitly set to either or upon entry to an object. But as with the cancelability state, on exit from an object that cancelability type should always be restored to its value on entry to the object.

Finally, only functions that are cancel-safe may be called from a thread that is asynchronously cancelable.

The function pthread_setcancelstate() may fail with:

[]
The specified state is not PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE.

The function pthread_setcanceltype() may fail with:

[]
The specified state is not PTHREAD_CANCEL_DEFERRED or PTHREAD_CANCEL_ASYNCHRONOUS.

pthread_cancel(3)

The pthread_testcancel() function conforms to ISO/IEC 9945-1:1996 (“POSIX.1”). The standard allows implementations to make many more functions cancellation points.

This manual page was written by David Leonard <d@openbsd.org> for the OpenBSD implementation of pthread_cancel(3).

June 11, 2013 Mac OS X 12