OSF DCE Application Development Guide--Introduction and Style Guide

Threads
some_object.num_waiters = some_object.num_waiters + 1;
/* Enter the exception handling block... */
TRY
/* Test the predicate condition... */
while (! some_object.data_available)
/* If the desired condition is not yet true, wait for */
/* it to become true. This next call also auto- */
/* matically releases the mutex... */
pthread_cond_wait(some_object.condition, some_object.mutex);
/* Code to access data_available goes here */
<...>
/* If a "cancel" exception occurs during the call to */
/* pthread_cond_wait(), the thread will resume */
/* execution in the FINALLY block following... */
FINALLY
/* Remove this thread from the total number of threads */
/* waiting for the condition... */
some_object.num_waiters = some_object.num_waiters - 1;
/* Release the mutex, and then continue with the */
/* exception --that is, cancel ... */
pthread_mutex_unlock(some_object.mutex);
ENDTRY
Note that in order to handle the cancel as an exception, you must #include the
pthread_exc.h header file rather than pthread.h; this allows you to use the DCE Threads
exception interface.
2.3.3.4 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.
124246 Tandem Computers Incorporated 2 13