Using the RWTTryLockGuard Template Class
The
RWTTryLockGuard template class defines a constructor that uses the resource member function
tryAcquire(), to conditionally acquire a resource. The
tryAcquire() function only acquires the resource when it is immediately available. It returns
true if the resource was successfully acquired and
false if the resource is currently owned by another thread. The destructor for this class releases the resource, but only if it was and is still acquired. The try-lock guard method
isAcquired() can be used to test whether or not the acquisition was successful.
Example 42 – Using a try-lock guard for conditional acquisition and release
{
RWTTryLockGuard<RWMutexLock> lock(mutex);
if (lock.isAcquired()) {
// Mutex is acquired!
}
else {
// Mutex was not acquired!
}
}
// Mutex is released (if acquired)!