RWTTryReadLockGuard<Resource> RWTLockGuardBase<Resource>
#include <rw/sync/RWTTryReadLockGuard.h>
RWTTryReadLockGuard<Resource> is a guard class that tries to acquire read access on its resource upon creation and, if successful, release it upon destruction. Guard objects work in conjunction with block statements in such a way as to establish an appropriate state upon creation, maintain that state for the duration of the block, and restore the original state upon destruction. For example, a guard may acquire read access on a mutex upon creation and release it when destructed. The class used as the actual template parameter for Resource must be one that provides tryAcquireRead and release methods (such as some synchronization classes provided by the library).
#include <rw/sync/RWReadersWriterLock.h> #include <rw/sync/RWTTryReadLockGuard.h> RWReadersWriterLock rwlock; RWBoolean foo() { // ... { // try to acquire read access on a readers-writer lock: RWTTryReadLockGuard<RWReadersWriterLock> lock(rwlock); // See if the lock was acquired: if (!lock.isAcquired()) { return FALSE; // Can't get lock, // give up and return error } // ... critical section // lock will be released in RWTTryReadLockGuard destructor // before leaving scope, especially important // if an exception might be thrown! } // ... return TRUE; }
typedef Resource ResourceType;
RWTTryReadLockGuard(Resource& resource);
Constructs an instance with the given resource and attempts to acquire read access on that resource. Uses the inherited isAcquired() member function to see if the acquisition was successful.
RWBoolean tryAcquire(void);
Try again to acquire read access on the resource. Uses this function instead of manipulating the resource directly so that the guard will automatically release it.
RWTTryReadLockGuard(const RWTTryReadLockGuard<Resource>& second);
Copy constructor is protected to disallow copy construction.
RWTTryReadLockGuard<Resource>& operator=(const RWTTryReadLockGuard<Resource>& second);
Assignment operator is protected to disallow assignment.
RWTReadLockGuard<Resource>, RWTReadUnlockGuard<Resource>
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.