OSF DCE Application Development Guide--Introduction and Style Guide
OSF DCE Application Development Guide—Introduction and Style Guide
Cleanup routines are also invoked when the thread calls pthread_exit( ). Cleanup
routines should never exit via longjmp( ) or siglongjmp( ).
2.3.3.5 Asynchronous Cancel Safety
A function is said to be asynchronous cancel safe if it is written in such a way that
entering the function with the cancelability state of asynchronous will not cause any
invariants to be violated if cancellation should occur at any (arbitrary) instruction. Such
functions are often written in such a manner that they need acquire no resources, and
variables which they write that are visible outside their process are strictly limited.
Any routines that acquire a resource can not be made asynchronous safe. This
unfortunately includes most routines that do useful work. The only function that is
guaranteed to be asysnchronous cancel safe is pthread_cancel( ). In general, no other
library functions should be called with cancelability state set to asynchronous.
2.3.3.6 Cancel Rules Summary
The following summarizes a set of cancel-related rules that should always be adhered to
when programming with cancels:
• Applications should not use cancels as a synchronization mechanism. Condition
variables should be used instead.
• pthread_mutex_lock( ) is not a cancellation point. Resources needing to be held
exclusively for a long time should be protected by condition variables rather than
mutexes, as this will not inhibit cancelability.
• A condition wait (via pthread_cond_wait( ) or pthread_cond_timedwait( ))isa
cancellation point. A side effect of acting on a cancellation request while in a
condition wait is that the mutex is (in effect) reacquired. The effect is as if the thread
were unblocked, allowed to execute up to the point of returning from the wait, but at
that point notices the cancellation request and handles it instead of returning.
• In general, most library calls cannot be assumed to be asynchronous cancel safe, and
hence must not be called with cancelability state set to asynchronous.
• Cleanup routines should never exit via longjmp( ) or siglongjmp().
In addition to the material covered in this section, Section 2.4 covers the additional
semantics of cancels as applied to RPC threads.
2.3.4 Signals
2− 14 Tandem Computers Incorporated 124246