SourcePro® API Reference Guide

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

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

#include <rw/sync/RWTTryLockGuard.h>

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

Public Member Functions

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

Private Member Functions

 RWTTryLockGuard (const RWTTryLockGuard< Resource > &second)
 
RWTTryLockGuard< Resource > & operator= (const RWTTryLockGuard< 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 (void) const
 

Detailed Description

template<class Resource>
class RWTTryLockGuard< Resource >

RWTTryLockGuard 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 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 when created 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.

Example
#include <rw/sync/RWMutexLock.h>
#include <rw/sync/RWTTryLockGuard.h>
bool foo() {
// ...
{
// try to acquire 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;
}
Implements a mutex, or mutual exclusion lock.
Definition RWMutexLock.h:231
Guard class that tries to acquire its resource upon creation and release it upon destruction.
Definition RWTTryLockGuard.h:72
See also
RWTLockGuard, RWTUnlockGuard

Constructor & Destructor Documentation

◆ RWTTryLockGuard() [1/2]

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

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()

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

Destructor.

◆ RWTTryLockGuard() [2/2]

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

Copy construction prohibited.

Member Function Documentation

◆ operator=()

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

Assignment prohibited.

◆ tryAcquire()

template<class Resource >
bool RWTTryLockGuard< Resource >::tryAcquire ( void )
inline

Tries again to acquire 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.