rwlogo
SourcePro C++ 12.0

SourcePro® C++ API Reference Guide



   SourcePro C++
Documentation Home

RWTTryReadLockGuard< Resource > Class Template Reference
[Synchronization]

Guard class that tries to acquire read access on its resource upon creation and release it upon destruction. More...

#include <rw/sync/RWTTryReadLockGuard.h>

Inheritance diagram for RWTTryReadLockGuard< Resource >:
RWTReadLockGuardBase< Resource > RWTReadGuardBase< Resource >

List of all members.

Public Member Functions

 RWTTryReadLockGuard (Resource &resource)
 ~RWTTryReadLockGuard ()
bool tryAcquire ()

Private Member Functions

 RWTTryReadLockGuard (const RWTTryReadLockGuard< Resource > &second)
RWTTryReadLockGuard< Resource > & operator= (const RWTTryReadLockGuard< Resource > &second)

Detailed Description

template<class Resource>
class RWTTryReadLockGuard< Resource >

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 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 when created 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 in this module.

Examples

 #include <rw/sync/RWReadersWriterLock.h>
 #include <rw/sync/RWTTryReadLockGuard.h>
 
 RWReadersWriterLock rwlock;
 
 bool 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;
 }
See also:
RWTReadLockGuard<Resource>

Constructor & Destructor Documentation

template<class Resource >
RWTTryReadLockGuard< Resource >::RWTTryReadLockGuard ( Resource &  resource  )  [inline]

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.

template<class Resource >
RWTTryReadLockGuard< Resource >::~RWTTryReadLockGuard (  )  [inline]

Destructor.

template<class Resource>
RWTTryReadLockGuard< Resource >::RWTTryReadLockGuard ( const RWTTryReadLockGuard< Resource > &  second  )  [private]

Copy construction prohibited.


Member Function Documentation

template<class Resource>
RWTTryReadLockGuard<Resource>& RWTTryReadLockGuard< Resource >::operator= ( const RWTTryReadLockGuard< Resource > &  second  )  [private]

Assignment prohibited.

template<class Resource >
bool RWTTryReadLockGuard< Resource >::tryAcquire (  )  [inline]

Tries again to acquire read access on the resource. Use this function instead of manipulating the resource directly so the guard will automatically release it.

 All Classes Functions Variables Typedefs Enumerations Enumerator Friends

© 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.