# include <ucr/Mutex.h> class Mutex; class Condition;
The Mutex class implements the more sophisticated monitor style of synchronization. It is more convenient to use then the previous synchronization primitives, but cannot be shared with interrupt handlers.
The basic idea of the Mutex class is to create a monitor protected by mutual exclusion. Only one thread at a time may run within a monitor. A thread enters a monitor by calling the Mutex::enter() method of the Mutex object, and leaves by calling Mutex::leave(). If another thread is running in the monitor, the call to Mutex::enter() method will block.
The Condition class implements condition variables associated with monitor mutexs. When a thread is running in a monitor, it can wait on a condition by calling Condition::wait(). This causes the thread to stop until another thread signals the condition with the Condition::signal() method. A thread blocked on a condition variable logically releases the monitor for other threads, but grabs the monitor again when it returns from the wait.
Th sample semaphore class in Figure is implemented with Mutex to demonstrate the important features of monitors. The Mutex variable protects the public methods from concurrent execution. A thread may block in the get, and other threads will only be allowed to enter the get or put when the blocked thread is waiting on the flag_ condition. At all times, only one thread is executing the protected code, although many threads may be stopped in the Mutex::enter() or Condition::wait() methods.