|
OpenTop 1.3 | |||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||
| SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD | |||||
#include "ot/base/Monitor.h"

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 |
void notify()
IllegalMonitorStateException - void notifyAll()
IllegalMonitorStateException - void wait()
IllegalMonitorStateException - InterruptedException - void wait(unsigned long millis)
millis - IllegalMonitorStateException - InterruptedException -
|
OpenTop 1.3 | |||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||
| SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD | |||||