Cross-Platform C++

ot
class Monitor

#include "ot/base/Monitor.h"

ot::SynchronizedObject ot::ManagedObject ot::Thread A base class providing both synchronization and notification. The Monitor class is based on the concept of a Mutex and a ConditionVariable. The Mutex is used to synchronize access to a shared object; the ConditionVariable is used to wake up waiting threads when the shared object has changed.

By combining a Mutex and ConditionVariable in this way, the Monitor class provides a simple and safe means for making a class thread-safe. However, it should be noted that this gain in simplicity is achieved with some loss of flexibility and, possibly, efficiency. In particular there is no requirement for a ConditionVariable to be associated with a single Mutex, nor is there a requirement for a Mutex to be associated with a single ConditionVariable.

As an example, we show a simple class that implements a thread-safe counter. For the purposes of demonstration, we have decided that whenever the count changes, all waiting threads should be woken so that they can re-inspect the count.

    class ActiveCounter : public Monitor {
    public:
        ActiveCounter() : m_count(0) {}

        void updateBy(long diff) {
            OT_SYNCHRONIZED  // creates a locked scope
            m_count+=diff;
            notifyAll();      // notifies all waiting threads
        }

        long getValue() const {
            OT_SYNCHRONIZED  // creates a locked scope
            return m_count;
        }

    private:
        long m_count;
    };

The OT_SYNCHRONIZED macro is described in the class description for SynchronizedObject.




Method Summary
 void notify()
         Wakes up one thread that is currently waiting on this Monitor.
 void notifyAll()
         Wakes up all threads that are currently waiting on this Monitor.
 void wait()
         Atomically releases the lock on this SynchronizedObject and waits for this Monitor to become notified for the current thread.
 void wait(unsigned long millis)
         Atomically releases the lock on this SynchronizedObject and waits for up to millis milliseconds for this Monitor to become notified for the current thread.

Methods inherited from class ot::ManagedObject
addRef, getRefCount, onFinalRelease, operator=, release

Methods inherited from class ot::SynchronizedObject
lock, unlock

Method Detail

notify

void notify()
Wakes up one thread that is currently waiting on this Monitor. Before calling this function, the caller must own the lock on this SynchronizedObject. On return from this function, the lock on this SynchronizedObject is still owned.

Exceptions:
IllegalMonitorStateException - if the caller does not own the lock on this SynchronizedObject
See also:
notifyAll()

notifyAll

void notifyAll()
Wakes up all threads that are currently waiting on this Monitor. Before calling this function, the caller must own the lock on this SynchronizedObject. On return from this function, the lock on this SynchronizedObject is still owned.

Exceptions:
IllegalMonitorStateException - if the caller does not own the lock on this SynchronizedObject
See also:
notify()

wait

void wait()
Atomically releases the lock on this SynchronizedObject and waits for this Monitor to become notified for the current thread. Before calling this function, the caller must own the lock on this SynchronizedObject. On return from this function, the lock on this SynchronizedObject is re-acquired.

Exceptions:
IllegalMonitorStateException - if the caller does not own the lock on this SynchronizedObject
InterruptedException - if the current thread is interrupted
See also:
notify() , notifyAll()

wait

void wait(unsigned long millis)
Atomically releases the lock on this SynchronizedObject and waits for up to millis milliseconds for this Monitor to become notified for the current thread. Before calling this function, the caller must own the lock on this SynchronizedObject. On return from this function, the lock on this SynchronizedObject is re-acquired.

Parameters:
millis - the number of milliseconds to wait
Exceptions:
IllegalMonitorStateException - if the caller does not own the lock on this SynchronizedObject
InterruptedException - if the current thread is interrupted
See also:
notify() , notifyAll()


Cross-Platform C++

Found a bug or missing feature? Please email us at support@elcel.com

Copyright © 2000-2003 ElCel Technology   Trademark Acknowledgements