rwlogo
SourcePro C++ 12.0

SourcePro® C++ API Reference Guide



   SourcePro C++
Documentation Home

RWTMonitor< Mutex > Class Template Reference
[Synchronization]

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 >

List of all members.

Protected Types

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

Protected Member Functions

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

Detailed Description

template<class Mutex>
class RWTMonitor< Mutex >

Subclassing from RWTMonitor<Mutex> provides a convenient way to synchronize the member functions used to access the data or resources encapsulated by a class. RWTMonitor<Mutex> 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<Mutex> class.

Examples

 #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!

    RWTHRASSERT(monitor().isAcquired());
 
    // Do something useful...
 }
See also:
RWMutexLock RWTLockGuard<Resource> RWTUnlockGuard<Resource> RWTTryLockGuard<Resource>

Member Typedef Documentation

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

Predefined guard for use with the monitor.

Reimplemented in RWTCountingBody< Mutex >, RWTThreadLocal< Type >, and RWTCountingBody< RWMutexLock >.

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

Predefined try lock guard for use with the monitor.

Reimplemented in RWTCountingBody< Mutex >, RWTThreadLocal< Type >, and RWTCountingBody< RWMutexLock >.

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

Predefined unlock guard for use with the monitor.

Reimplemented in RWTCountingBody< Mutex >, RWTThreadLocal< Type >, and RWTCountingBody< RWMutexLock >.


Constructor & Destructor Documentation

template<class Mutex >
RWTMonitor< Mutex >::RWTMonitor (  )  [inline, protected]

Constructs a default monitor instance, initializing the mutex.

template<class Mutex >
RWTMonitor< Mutex >::RWTMonitor ( RWStaticCtor   )  [inline, protected]

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  )  [inline, protected]

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 (  )  [inline, protected]

Destructor.


Member Function Documentation

template<class Mutex >
void RWTMonitor< Mutex >::acquire ( void   )  [inline, protected]
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 only available when you build the debug version of the module.

template<class Mutex >
RWTMonitor< Mutex > & RWTMonitor< Mutex >::monitor (  )  const [inline, protected]

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   )  [inline, protected]

Returns the monitor's mutex.

template<class Mutex>
RWTMonitor< Mutex > & RWTMonitor< Mutex >::operator= ( const RWTMonitor< Mutex > &   )  [inline, protected]

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   )  [inline, protected]
template<class Mutex >
bool RWTMonitor< Mutex >::tryAcquire (  )  [inline, protected]

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

 All Classes Functions Variables Typedefs Enumerations Enumerator Friends

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.