8.6. Pthread cancellation

POSIX provides facilities for canceling Pthreads. The facilities enable a Pthread to cancel another specified Pthread within the same process. The facilities are aimed at allowing applications to cancel cooperating Pthreads.

A Pthread that is the target of cancellation must be cooperative in the following sense. Each Pthread controls its own cancelability state and type. The cancelability state can be enabled or disabled. For the enabled state, the type can be asynchronous (cancellation requests accepted at any time) or deferred (cancellation accepted only at designated cancellation points). By default, Pthreads are initialized with state enabled and type deferred. An uncooperative Pthread could disable cancellation, and thus thwart cancellation requests.

An application is expected to make cancellation graceful by specifying cancellation cleanup handlers. The cleanup handlers perform actions such as unlocking mutexes owned by the target Pthread (the Pthread being canceled), signaling conditions that the target Pthread may have caused to become true, releasing resources, and so on. These actions enable other Pthreads in the application to make progress after the cancellation occurs.

The Pthread cancellation facilities specified in POSIX include the following functions:

  • Canceling execution of a specified Pthread using the pthread_cancel() routine. The cancellation occurs in accordance with the target Pthread’s cancelability state and type. If and when cancellation does occur, cancellation cleanup handlers are invoked in last-in-first-out (LIFO) order, followed by thread-specific data destructor functions in unspecified order. Then the Pthread is terminated.

  • Setting the cancelability state of the calling Pthread using the pthread_setcancelstate() routine to either enabled or disabled.

  • Setting the cancelability type of the calling Pthread using pthread_setcanceltype() to either asynchronous or deferred.

  • Creating a cancellation point using the pthread_testcancel() routine. To designate a point in its execution as a cancellation point, a Pthread simply makes a call to the pthread_testcancel() function at that point.

  • Establishing cancellation cleanup handlers using the pthread_cleanup_push() and pthread_cleanup_pop() routines. The pthread_cleanup_push() function is used to identify a specified routine as a cleanup handler. Conceptually, the routine is pushed onto the top of the calling Pthread’s cancellation cleanup stack. The pthread_cleanup_pop() function is used to remove and optionally execute the cleanup handler at the top of the cancellation cleanup stack.

In order to determine which subroutines provided by the AIX base operating system can be canceled (cancellation point(s) can occur when a Pthread is executing that subroutine), please refer to the “Thread Programming Guidelines” section in AIX 5L Version 5.2 General Programming Concepts: Writing and Debugging Programs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset