SourcePro® API Reference Guide

 
List of all members | Public Member Functions | Private Member Functions
RWTLockGuard< Resource > Class Template Reference

A guard that acquires its resource upon creation and releases it upon destruction. More...

#include <rw/sync/RWTLockGuard.h>

Inheritance diagram for RWTLockGuard< Resource >:
RWTLockGuardBase< Resource > RWTGuardBase< Resource >

Public Member Functions

 RWTLockGuard (Resource &resource)
 
 ~RWTLockGuard ()
 
- Public Member Functions inherited from RWTLockGuardBase< Resource >
 ~RWTLockGuardBase ()
 
- Public Member Functions inherited from RWTGuardBase< Resource >
 ~RWTGuardBase ()
 
void acquire ()
 
bool isAcquired () const
 
void release ()
 

Private Member Functions

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

Additional Inherited Members

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

Detailed Description

template<class Resource>
class RWTLockGuard< Resource >

RWTLockGuard is a guard class that acquires its resource upon creation and releases 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 a mutex upon creation and release it when destructed. The class used as the actual template parameter for Resource must be one that provides acquire() and release() methods, such as provided by the Threads Module synchronization classes.

Example
#include <rw/sync/RWMutexLock.h>
#include <rw/sync/RWTLockGuard.h>
// ...
void foo()
{
RWTLockGuard<RWMutexLock> lock(mutex); // acquires mutex
// ... critical section
// Mutex will be released in RWTLockGuard destructor
// before leaving scope, especially important if an exception
// might be thrown!
}
See also
RWTTryLockGuard, RWTUnlockGuard

Constructor & Destructor Documentation

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

Constructs a guard instance with the given resource and acquires that resource. The resource is released in the destructor of the base class.

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

Destructor.

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

Copy construction prohibited.

Member Function Documentation

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

Assignment prohibited.

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