SourcePro® API Reference Guide

 
Loading...
Searching...
No Matches
RWTTryReadLockGuard< Resource > Class Template Reference

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 >

Public Member Functions

 RWTTryReadLockGuard (Resource &resource)
 
 ~RWTTryReadLockGuard (void)
 
bool tryAcquire (void)
 
- Public Member Functions inherited from RWTReadLockGuardBase< Resource >
 ~RWTReadLockGuardBase (void)
 
- Public Member Functions inherited from RWTReadGuardBase< Resource >
 ~RWTReadGuardBase (void)
 
void acquire (void)
 
bool isAcquired (void) const
 
void release (void)
 

Private Member Functions

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

Additional Inherited Members

- Protected Member Functions inherited from RWTReadLockGuardBase< Resource >
 RWTReadLockGuardBase (Resource &resource)
 
 RWTReadLockGuardBase (Resource &resource, bool acquired)
 
- Protected Member Functions inherited from RWTReadGuardBase< Resource >
 RWTReadGuardBase (Resource &resource)
 
 RWTReadGuardBase (Resource &resource, bool acquired)
 
Resource & resource (void) const
 

Detailed Description

template<class Resource>
class RWTTryReadLockGuard< Resource >

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

Example
#include <rw/sync/RWReadersWriterLock.h>
#include <rw/sync/RWTTryReadLockGuard.h>
bool foo() {
// ...
{
// try to acquire read 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 RWTTryReadLockGuard destructor
// before leaving scope, especially important
// if an exception might be thrown!
}
// ...
return true;
}
A synchronization lock that allows concurrent access to multiple readers, but limits access to a sing...
Definition RWReadersWriterLock.h:127
Guard class that tries to acquire read access on its resource upon creation and release it upon destr...
Definition RWTTryReadLockGuard.h:73
See also
RWTReadLockGuard

Constructor & Destructor Documentation

◆ RWTTryReadLockGuard() [1/2]

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.

◆ ~RWTTryReadLockGuard()

template<class Resource >
RWTTryReadLockGuard< Resource >::~RWTTryReadLockGuard ( void )
inline

Destructor.

◆ RWTTryReadLockGuard() [2/2]

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

Copy construction prohibited.

Member Function Documentation

◆ operator=()

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

Assignment prohibited.

◆ tryAcquire()

template<class Resource >
bool RWTTryReadLockGuard< Resource >::tryAcquire ( void )
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.

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