Using Synchronization Classes to Service Cancellation Requests
The Threads Module synchronization classes can automatically service cancellation requests before performing any operation that may result in blocking the calling thread.
To use this capability, a synchronization object must have its cancellation processing enabled. You can do this when the synchronization object is constructed by passing the appropriate RWCancellationState value to the constructor. The default cancellation state of a synchronization object is RW_CANCELLATION_DISABLED. You can override that default, however, by specifying RW_CANCELLATION_ENABLED when you construct the synchronization instance, as shown in Example 10.
Example 10 – Constructing synchronization objects to automatically service cancellation requests
RWMutexLock mutex(RW_CANCELLATION_ENABLED);
 
try {
mutex.acquire(); // May throw cancellation!
}
catch(RWCancellation&) {
// Do something about it!
}
Except when using nested lockguards, you can also enable synchronization cancellation processing by using the setCancellation(RWCancellationState) member function provided by RWSynchObject class, the base class for all synchronization mechanisms in Threads Module. Example 11 shows this approach.
Example 11 – Setting synchronization objects to automatically service cancellation requests
RWMutexLock mutex; // Defaults to disabled!
 
mutex.setCancellation(RW_CANCELLATION_ENABLED);
try {
mutex.acquire(); // May throw cancellation!
}
catch(RWCancellation&) {
// Do something about it!
}
Figure 13 shows the interaction and state changes associated with the cancellation process.
Figure 13 – Cancel operations — interaction and timing