PreviousNext

Thread Cleanup

Each thread maintains a list of cleanup routines (handlers). The routines are placed on and removed from the list by the pthread_cleanup_push( ) and pthread_cleanup_pop( ) functions, respectively. These functions must appear as statements and in pairs within the same lexical scope.

When a cancellation request is acted upon, the routines on the list are invoked in the last in, first out (LIFO) order with cancellation disabled (cancelability state of deferred) until the last cleanup routine returns. When the last cleanup routine returns, thread execution is terminated. If other routines are joining with the target of the cancellation, a status of (void*) -1 is made available to them.

Cleanup routines are also invoked when the thread calls pthread_exit( ). Cleanup routines should never exit via longjmp( ) or siglongjmp( ).