SourcePro C++ 12.0 |
SourcePro® C++ API Reference Guide |
SourcePro C++ Documentation Home |
Supplies the mutual exclusion and guard mechanisms for synchronizing member functions. More...
#include <rw/sync/RWTMonitor.h>
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 () |
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.
#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... }
typedef RWTLockGuard< RWTMonitor< Mutex > > RWTMonitor< Mutex >::LockGuard [protected] |
Predefined guard for use with the monitor.
Reimplemented in RWTCountingBody< Mutex >, RWTThreadLocal< Type >, and RWTCountingBody< RWMutexLock >.
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 >.
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 >.
RWTMonitor< Mutex >::RWTMonitor | ( | ) | [inline, protected] |
Constructs a default monitor instance, initializing the 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.
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.
RWTMonitor< Mutex >::~RWTMonitor | ( | ) | [inline, protected] |
Destructor.
void RWTMonitor< Mutex >::acquire | ( | void | ) | [inline, protected] |
Locks the monitor by acquiring the monitor's mutex.
Reimplemented in RWByteInputStreamImp, RWByteOutputStreamImp, RWCharInputStreamImp, RWCharOutputStreamImp, RWDataInputStreamImp, RWDataOutputStreamImp, RWSynchronizedDataInputStreamImp, RWSynchronizedDataOutputStreamImp, RWUCharInputStreamImp, RWUCharOutputStreamImp, RWWCharInputStreamImp, RWWCharOutputStreamImp, RWTInputStreamLockImp< RWUCharInputStream, RWFilteredUCharInputStreamImp >, RWTInputStreamLockImp< RWWCharInputStream, RWFilteredWCharInputStreamImp >, RWTInputStreamLockImp< RWCharInputStream, RWFilteredCharInputStreamImp >, and RWTInputStreamLockImp< RWByteInputStream, RWFilteredByteInputStreamImp >.
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.
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.
Mutex & RWTMonitor< Mutex >::mutex | ( | void | ) | [inline, protected] |
Returns the monitor's 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.
void RWTMonitor< Mutex >::release | ( | void | ) | [inline, protected] |
Unlocks the monitor by releasing the monitor's mutex.
Reimplemented in RWByteInputStreamImp, RWByteOutputStreamImp, RWCharInputStreamImp, RWCharOutputStreamImp, RWDataInputStreamImp, RWDataOutputStreamImp, RWSynchronizedDataInputStreamImp, RWSynchronizedDataOutputStreamImp, RWUCharInputStreamImp, RWUCharOutputStreamImp, RWWCharInputStreamImp, RWWCharOutputStreamImp, RWTInputStreamLockImp< RWUCharInputStream, RWFilteredUCharInputStreamImp >, RWTInputStreamLockImp< RWWCharInputStream, RWFilteredWCharInputStreamImp >, RWTInputStreamLockImp< RWCharInputStream, RWFilteredCharInputStreamImp >, and RWTInputStreamLockImp< RWByteInputStream, RWFilteredByteInputStreamImp >.
bool RWTMonitor< Mutex >::tryAcquire | ( | ) | [inline, protected] |
Conditionally locks the monitor if the monitor can be locked without blocking.
© 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.