SourcePro® API Reference Guide

 
List of all members | Protected Types | Protected Member Functions
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 ()
 
 RWTMonitor (RWStaticCtor)
 
 RWTMonitor (const RWTMonitor< Mutex > &second)
 
 ~RWTMonitor ()
 
void acquire ()
 
bool isAcquired () const
 
RWTMonitor< Mutex > & monitor () const
 
Mutex & mutex ()
 
RWTMonitor< Mutex > & operator= (const RWTMonitor< Mutex > &)
 
void release ()
 
bool tryAcquire ()
 

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/RWTMonitor.h>
#include <rw/sync/RWMutexLock.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...
}
See also
RWMutexLock, RWTLockGuard, RWTUnlockGuard, RWTTryLockGuard

Member Typedef Documentation

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

Predefined guard for use with the monitor.

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

Predefined try lock guard for use with the monitor.

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

Predefined unlock guard for use with the monitor.

Constructor & Destructor Documentation

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

Constructs a default monitor instance, initializing the mutex.

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.

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.

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

Destructor.

Member Function Documentation

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

Locks the monitor by acquiring the monitor's mutex.

template<class Mutex>
bool RWTMonitor< Mutex >::isAcquired ( ) 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.

template<class Mutex >
RWTMonitor< Mutex > & RWTMonitor< Mutex >::monitor ( ) 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.

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

Returns the monitor's mutex.

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.

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

Unlocks the monitor by releasing the monitor's mutex.

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

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

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