Module: Threads Package: Synchronization
RWTTryLockGuard<Resource> RWTLockGuardBase<Resource>
#include <rw/sync/RWTTryLockGuard.h>
RWTTryLockGuard<Resource> is a guard class that tries to acquire 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 a mutex upon creation and release it when destructed. The class used as the actual template parameter for Resource must be one that provides tryAcquire and release methods (such as some synchronization classes provided in this module).
#include <rw/sync/RWMutexLock.h> #include <rw/sync/RWTTryLockGuard.h> RWMutexLock mutex; bool foo() { // ... { // try to acquire mutex: RWTTryLockGuard<RWMutexLock> lock(mutex); // See if the mutex was acquired: if (!lock.isAcquired()) { return false; // Can't get lock, // give up and return error } // ... critical section // Mutex will be released in RWTTryLockGuard destructor // before leaving scope, especially important // if an exception might be thrown! } // ... return true; }
typedef Resource ResourceType;
RWTTryLockGuard(Resource& resource);
Constructs an instance with the given resource and attempts to acquire that resource. Use the inherited isAcquired() member function to see if the acquisition was successful.
~RWTTryLockGuard();
Destructor.
bool tryAcquire();
Tries again to acquire the resource. Use this function instead of manipulating the resource directly so that the guard will automatically release it.
RWTTryLockGuard(const RWTTryLockGuard<Resource>& second);
Copy construction prohibited.
RWTTryLockGuard<Resource>& operator=(const RWTTryLockGuard<Resource>& second);
Assignment prohibited.
RWTLockGuard<Resource>, RWTUnlockGuard<Resource>
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.