Servicing Cancellation Requests
The synchronization classes can automatically service cancellation requests before performing any operation that can result in blocking the calling thread. If you want to do this, enable cancellation processing for the synchronization object when you construct it 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 by specifying the Threading package
RW_CANCELLATION_ENABLED state when you construct the synchronization instance, as shown in
Example 34.
Example 34 – Enabling cancellation request service at instantiation
RWMutexLock mutex(RW_CANCELLATION_ENABLED);
try {
mutex.acquire(); // May throw cancellation!
}
catch(RWCancellation&) {
// Do something about it!
}
You can also enable and disable synchronization cancellation processing as shown in
Example 35. This approach uses the
setCancellation() member function included in the
RWSynchObject class, which is the base class for all synchronization mechanisms.
Example 35 – Enabling automatic servicing of cancellation requests
#include <rw/thread/RWCancellation.h>
RWMutexLock mutex; // Defaults to disabled!
mutex.setCancellation(RW_CANCELLATION_ENABLED);
try {
mutex.acquire(); // May throw cancellation!
}
catch(RWCancellation&) {
// Do something about it!
}
This example requires the Threading package. See
Chapter 3, The Threading Package, for more information on cancellation.