SourcePro® API Reference Guide

 
Loading...
Searching...
No Matches
RWTMonitor< Mutex > Class Template Reference

Supplies the mutual exclusion and guard mechanisms for synchronizing member functions. More...

#include <rw/sync/RWTMonitor.h>

Inheritance diagram for RWTMonitor< Mutex >:
RWTCountingBody< Mutex > RWTCounter< Mutex >

Protected Types

typedef RWTLockGuard< RWTMonitor< Mutex > > LockGuard
 
typedef RWTTryLockGuard< RWTMonitor< Mutex > > TryLockGuard
 
typedef RWTUnlockGuard< RWTMonitor< Mutex > > UnlockGuard
 

Protected Member Functions

 RWTMonitor (const RWTMonitor< Mutex > &second)
 
 RWTMonitor (RWStaticCtor)
 
 RWTMonitor (void)
 
 ~RWTMonitor (void)
 
void acquire (void)
 
bool isAcquired (void) const
 
RWTMonitor< Mutex > & monitor (void) const
 
Mutex & mutex (void)
 
RWTMonitor< Mutex > & operator= (const RWTMonitor< Mutex > &)
 
void release (void)
 
bool tryAcquire (void)
 

Detailed Description

template<class Mutex>
class RWTMonitor< Mutex >

Subclassing from RWTMonitor provides a convenient way to synchronize the member functions used to access the data or resources encapsulated by a class. RWTMonitor supplies the mutual exclusion and guard mechanisms for synchronizing member functions. Functions that need to be synchronized use the supplied guard type to lock the monitor upon entry and unlock it upon exit from the function.

This class provides an alternative to directly inheriting from or including a synchronization class to achieve the same purpose. If you use your own mutex for synchronization, you must cast away the const-ness of this in const member functions before acquiring or releasing the mutex.

By using this class, you have access to the monitor() function, eliminates the above inconvenience by always returning a non-const instance of the monitor. The reference returned by monitor() can be used to initialize any of the public guard types defined by the RWTMonitor class.

Example
#include <rw/sync/RWMutexLock.h>
#include <rw/sync/RWTMonitor.h>
class MyClass : public RWTMonitor<RWMutexLock> {
public:
void myFunc1(void); // Synchronized Function (Non-atomic)
void myFunc2(void) const; // Synchronized Function (Atomic)
private:
void helper(void) const; // Assumes lock is already
// acquired!
};
void MyClass::myFunc1(void) {
// Use a guard to lock the monitor (and automatically unlock it)
LockGuard lock(monitor());
// Do something useful...
{
// Temporarily unlock the monitor so you can call another
// synchronized function
UnlockGuard unlock(monitor());
// Call another synchronized function
myFunc2();
// Monitor automatically re-locked upon exit from block
}
// Do some more stuff
}
void MyClass::myFunc2(void) const {
// Use a guard to lock the monitor (and automatically unlock it)
LockGuard lock(monitor());
// Call a private helper function
helper();
}
void MyClass::helper(void) const {
// Debug Mode - Make sure the monitor is acquired by this thread!
RW_ASSERT(monitor().isAcquired());
// Do something useful...
}
Supplies the mutual exclusion and guard mechanisms for synchronizing member functions.
Definition RWTMonitor.h:107
RWTLockGuard< RWTMonitor< Mutex > > LockGuard
Definition RWTMonitor.h:196
RWTMonitor< Mutex > & monitor(void) const
Definition RWTMonitor.h:280
RWTUnlockGuard< RWTMonitor< Mutex > > UnlockGuard
Definition RWTMonitor.h:201
bool isAcquired(void) const
See also
RWMutexLock, RWTLockGuard, RWTUnlockGuard, RWTTryLockGuard

Member Typedef Documentation

◆ LockGuard

template<class Mutex >
typedef RWTLockGuard<RWTMonitor<Mutex> > RWTMonitor< Mutex >::LockGuard
protected

Predefined guard for use with the monitor.

◆ TryLockGuard

template<class Mutex >
typedef RWTTryLockGuard<RWTMonitor<Mutex> > RWTMonitor< Mutex >::TryLockGuard
protected

Predefined try lock guard for use with the monitor.

◆ UnlockGuard

template<class Mutex >
typedef RWTUnlockGuard<RWTMonitor<Mutex> > RWTMonitor< Mutex >::UnlockGuard
protected

Predefined unlock guard for use with the monitor.

Constructor & Destructor Documentation

◆ RWTMonitor() [1/3]

template<class Mutex >
RWTMonitor< Mutex >::RWTMonitor ( void )
inlineprotected

Constructs a default monitor instance, initializing the mutex.

◆ RWTMonitor() [2/3]

template<class Mutex >
RWTMonitor< Mutex >::RWTMonitor ( RWStaticCtor )
inlineprotected

Constructs a static monitor instance. This constructor does not initialize the mutex; rather, the mutex is initialized the first time it is accessed.

◆ RWTMonitor() [3/3]

template<class Mutex >
RWTMonitor< Mutex >::RWTMonitor ( const RWTMonitor< Mutex > & second)
inlineprotected

This copy constructor does not actually copy anything (mutexes generally cannot be copied). It is provided so you can define a copy constructor in your derived class.

◆ ~RWTMonitor()

template<class Mutex >
RWTMonitor< Mutex >::~RWTMonitor ( void )
inlineprotected

Destructor.

Member Function Documentation

◆ acquire()

template<class Mutex >
void RWTMonitor< Mutex >::acquire ( void )
inlineprotected

Locks the monitor by acquiring the monitor's mutex.

◆ isAcquired()

template<class Mutex >
bool RWTMonitor< Mutex >::isAcquired ( void ) const
protected

Determines whether the calling thread currently owns the monitor.

This function is primarily intended for use in precondition assertions, and is available only when you build the debug version of the module.

◆ monitor()

template<class Mutex >
RWTMonitor< Mutex > & RWTMonitor< Mutex >::monitor ( void ) const
inlineprotected

Casts away const and returns a reference to self. This allows you to avoid explicitly casting away const on the this pointer in every const member function that needs to lock the monitor.

◆ mutex()

template<class Mutex >
Mutex & RWTMonitor< Mutex >::mutex ( void )
inlineprotected

Returns the monitor's mutex.

◆ operator=()

template<class Mutex >
RWTMonitor< Mutex > & RWTMonitor< Mutex >::operator= ( const RWTMonitor< Mutex > & )
inlineprotected

This assignment operator does not actually assign anything (mutexes generally cannot be assigned). It is provided so you can define an assignment operator in your derived class.

◆ release()

template<class Mutex >
void RWTMonitor< Mutex >::release ( void )
inlineprotected

Unlocks the monitor by releasing the monitor's mutex.

◆ tryAcquire()

template<class Mutex >
bool RWTMonitor< Mutex >::tryAcquire ( void )
inlineprotected

Conditionally locks the monitor if the monitor can be locked without blocking.

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