The RWTMonitor<Lock> Class
To simplify the development of monitor classes, the Synchronization package defines a template class,
RWTMonitor<Lock>, that can be used as a base class in new monitor class implementations. This class includes a mutex member and has public definitions for
LockGuard and
UnlockGuard types that accept a self-reference returned by a
monitor() member function.
Using the
RWTMonitor as a base class gives the implementation in
Example 30.
Example 30 – Deriving from RWTMonitor
#include <rw/sync/RWMutexLock.h>
#include <rw/sync/RWTMonitor.h>
class Counter : public RWTMonitor<RWMutexLock> {
private:
int count;
public:
Counter(int count=0) : count(count) {}
Counter& operator++(void)
{
RWMutexLock::LockGuard lock(monitor());
count++;
return *this;
}
Counter& operator--(void)
{
RWMutexLock::LockGuard lock(monitor());
count--;
return *this;
}
operator int(void) const
{
RWMutexLock::LockGuard lock(monitor());
return count;
}
};