Using a Lock Guard
To use a lock guard, declare a named instance of the guard class and initialize it with a reference to the synchronization mechanism, as shown in Example 39.
Example 39 – Using a lock guard
RWMutexLock mutex;
 
{
RWTLockGuard<RWMutexLock> lock(mutex); // 1
// Mutex is acquired!
} // 2
// Mutex is released!
//1 The declaration of an RWTLockGuard<RWMutexLock> instance invokes a constructor that acquires the mutex passed as an argument.
//2 The RWTLockGuard<RWMutexLock> instance is automatically destroyed when it goes out-of-scope—upon exit from the block where it was declared. The RWTLockGuard<RWMutexLock> destructor releases the mutex that was previously acquired. The guard instance is also destroyed if an exception occurs within this block, thus insuring that the mutex is always released.