Base Class
#include <rw/sync/RWTMonitor.h>
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; any functions that must be synchronized use the supplied guard type to lock and unlock the monitor upon entry and exit from the function.
One advantage of using RWTMonitor<Mutex> as a base-class instead of directly inheriting from or including some synchronization class, is the monitor() member function. If you were to use your own mutex member for synchronization, you would have to cast away the const-ness of this in const member functions before acquiring or releasing the mutex. The monitor() function eliminates this inconvenience by always returning a non-const instance of the monitor. The reference returned by monitor() can be used to initialize any of 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 // we 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()); helper(); // Call a private helper function } void MyClass::helper(void) const { // Debug Mode - Make sure the monitor // is acquired by this thread! RWTHRASSERT(monitor().isAcquired()); // Do something useful... }
typedef Mutex MutexType;
typedef RWTLockGuard< RWTMonitor< Mutex > > LockGuard; typedef RWTUnlockGuard< RWTMonitor< Mutex > > UnlockGuard; typedef RWTTryLockGuard< RWTMonitor< Mutex > > TryLockGuard;
Predefined guards for use with monitors.
RWTMonitor(void);
Constructs a default instance, initializing the mutex.
RWTMonitor(RWStaticCtor);
Constructs a static instance. This constructor does not initialize the mutex; the mutex will be initialized the first time it is accessed.
RWTMonitor(const RWTMonitor<Mutex>& second);
This copy constructor does not actually copy anything (mutexes generally cannot be copied). It is provided so that you can define a copy constructor in your derived class.
RWTMonitor<Mutex>& operator=(const RWTMonitor<Mutex>&);
This assignment operator does not actually assign anything (mutexes generally cannot be assigned). It is provided so that you can define an assignment operator in your derived class.
void acquire(void);
Locks the monitor by acquiring the monitor's mutex.
RWBoolean isAcquired(void) const;
Determines whether the calling thread currently owns the monitor.
NOTE: This function is primarily intended for use in precondition assertions, and is only available when using the debug version of the library.
RWTMonitor<Mutex>& monitor(void) const;
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& mutex(void);
Returns the monitor's mutex.
void release(void);
Unlocks the monitor by releasing the monitor's mutex.
RWBoolean tryAcquire(void):
Conditionally locks the monitor if the monitor can be locked without blocking.
RWMutexLock, RWTLockGuard<Resource>, RWTUnlockGuard<Resource>, RWTTryLockGuard<Resource>
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.