SourcePro® API Reference Guide

Product Documentation:
   SourcePro
Documentation Home
List of all members | Public Member Functions | Private Member Functions
RWTTryWriteLockGuard< Resource > Class Template Reference

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

#include <rw/sync/RWTTryWriteLockGuard.h>

Inheritance diagram for RWTTryWriteLockGuard< Resource >:
RWTWriteLockGuardBase< Resource > RWTWriteGuardBase< Resource >

Public Member Functions

 RWTTryWriteLockGuard (Resource &resource)
 
 ~RWTTryWriteLockGuard ()
 
bool tryAcquire ()
 
- Public Member Functions inherited from RWTWriteLockGuardBase< Resource >
 ~RWTWriteLockGuardBase ()
 
- Public Member Functions inherited from RWTWriteGuardBase< Resource >
 ~RWTWriteGuardBase ()
 
void acquire ()
 
bool isAcquired () const
 
void release ()
 

Private Member Functions

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

Additional Inherited Members

- Protected Member Functions inherited from RWTWriteLockGuardBase< Resource >
 RWTWriteLockGuardBase (Resource &resource)
 
 RWTWriteLockGuardBase (Resource &resource, bool acquired)
 
- Protected Member Functions inherited from RWTWriteGuardBase< Resource >
 RWTWriteGuardBase (Resource &resource)
 
 RWTWriteGuardBase (Resource &resource, bool acquired)
 
Resource & resource () const
 

Detailed Description

template<class Resource>
class RWTTryWriteLockGuard< Resource >

RWTTryWriteLockGuard is a guard class that tries to acquire write 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 write 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 tryAcquireWrite() and release() methods, such as some synchronization classes provided in this module.

Example
#include <rw/sync/RWReadersWriterLock.h>
#include <rw/sync/RWTTryWriteLockGuard.h>
bool foo()
{
// ...
{
// try to acquire write access on a readers-writer lock:
// 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 RWTTryWriteLockGuard destructor
// before leaving scope, especially important if
// an exception might be thrown!
}
// ...
return true;
}
See also
RWTWriteLockGuard

Constructor & Destructor Documentation

template<class Resource >
RWTTryWriteLockGuard< Resource >::RWTTryWriteLockGuard ( Resource &  resource)
inline

Constructs an instance with the given resource and attempts to acquire write access on that resource. Uses the inherited isAcquired() member function to see if the acquisition was successful.

template<class Resource >
RWTTryWriteLockGuard< Resource >::~RWTTryWriteLockGuard ( )
inline

Destructor.

template<class Resource>
RWTTryWriteLockGuard< Resource >::RWTTryWriteLockGuard ( const RWTTryWriteLockGuard< Resource > &  second)
private

Copy construction prohibited.

Member Function Documentation

template<class Resource>
RWTTryWriteLockGuard<Resource>& RWTTryWriteLockGuard< Resource >::operator= ( const RWTTryWriteLockGuard< Resource > &  second)
private

Assignment prohibited.

template<class Resource >
bool RWTTryWriteLockGuard< Resource >::tryAcquire ( )
inline

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

Copyright © 2021 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.