OSF DCE Application Development Guide--Introduction and Style Guide

OSF DCE Application Development Guide—Introduction and Style Guide
2.3.3.3 Cancellation Side Effects
Cancellation ordinarily involves cleanup in order to leave resources in an orderly state.
Any side effects of acting upon a cancellation request occur before the first cleanup
routine is called.
There are no side effects of acting upon a cancellation request while executing
pthread_join( ).
The side effects of acting upon a cancellation request while in a condition variable wait
are as follows:
The mutex is reacquired before calling the first cleanup routine.
In addition, while the thread is no longer considered to be waiting for the condition,
no signals directed at the condition variable are consumed by the target thread if
there are other threads blocked on the condition variable.
2.3.3.3.1 Using pthread_cancel( ) to Terminate a Thread
The pthread_cancel( ) routine allows a thread to cancel itself or another thread. The
routine is fully described in the pthread_cancel(3thr) reference page. Its use is
straightforward, but if you use it to cancel a thread that makes use of mutexes or
condition variables, you should keep in mind the following aspect of its operation.
The canceled thread receives the cancel in the form of an exception. If the thread has not
disabled its cancelability by a call to pthread_setcancel(), its effect is to immediately
terminate the thread. However, if the thread happens to have acquired a mutex
(including the global lock) when it is canceled, the mutex will remain in its locked state
and no other thread will be able to acquire it. Moreover, the data that was protected by
the mutex may be in an inconsistent state as a result of the thread’s having been canceled
in the middle of its operation on the data.
The easiest way to prevent this is simply to disable cancels before entering code for
which access has been restricted by a mutex. If this is undesirable, you can explicitly
handle a cancel by coding an exception-handling block.
This same possibility exists with condition variables, since the variable is protected by a
mutex. An example of handling a cancel (or any other exception) while using a
condition variable follows.
#include <pthread_exc.h>
<...>
/* First, lock the mutex that protects the condition variable */
/* and the predicate... */
pthread_mutex_lock(some_object.mutex);
/* Add this thread to the total number of threads waiting for */
/* the condition... */
2 12 Tandem Computers Incorporated 124246