Sub topic 15
Monitors and Condition Variables
Assume you know process and thread basics, context switching, critical sections, and semaphore operations. This chapter explains a higher-level way to protect shared data: a monitor with condition variables.
What a monitor provides
A monitor is a programming-language or operating-system construct that combines:
- Shared data.
- Procedures that access the data.
- Mutual exclusion.
- Condition variables for waiting until a state becomes true.
Only one thread can execute inside a monitor at a time. A thread entering any monitor procedure automatically acquires the monitor's lock. It releases the lock when the procedure returns.
This prevents callers from accidentally accessing the protected data without taking the lock.
| Feature | Semaphore | Monitor |
|---|---|---|
| Mutual exclusion | Must be programmed explicitly | Built into monitor entry |
| Waiting for a state | `wait` on a chosen semaphore | `wait` on a condition variable |
| Data protection | Separate from the data | Bundled with the data and procedures |
| Typical error | Wrong order or missing operation | Forgetting to signal a condition |
| Abstraction level | Lower | Higher |
A monitor does not make every operation safe automatically. Code outside the monitor must use its procedures rather than changing the protected data directly.
Condition variables
A condition variable represents a condition that a thread may need to wait for. Common examples include:
not_empty: a buffer contains at least one item.not_full: a buffer has at least one free slot.available: a resource can be allocated.
A condition variable normally supports two operations:
wait(condition): atomically releases the monitor lock and blocks the calling thread.signal(condition): wakes one thread waiting on that condition, if one exists.
The word atomically matters. The thread must release the lock and enter the waiting state as one indivisible action. Otherwise, another thread could change the state between those actions, and the waiting thread could miss the notification.
A waiting thread must check the condition in a loop:
monitor BoundedBuffer {
item buffer[N];
int count = 0;
condition not_empty;
condition not_full;
procedure put(item x) {
while (count == N)
wait(not_full);
insert(x);
count++;
signal(not_empty);
}
procedure get() returns item {
while (count == 0)
wait(not_empty);
item x = remove();
count--;
signal(not_full);
return x;
}
}The loop is necessary because a thread awakened by signal may not run immediately. Another thread might consume the available item first. Some systems can also wake a thread without a matching signal, a behavior called a spurious wake-up.
Signal semantics
Monitor designs commonly use one of two signal rules.
- Hoare-style semantics:
signalimmediately transfers control to the awakened thread. The signalling thread waits. - Mesa-style semantics:
signalmakes the waiting thread ready, but the signalling thread continues until it leaves or waits. The awakened thread must check the condition again.
Most modern systems use Mesa-style behavior. Therefore, use while, not if, around condition-variable waits.
Choosing monitors or semaphores
Semaphores can express many synchronization patterns, including resource counting. Monitors make ownership of shared data clearer and reduce the chance of using the wrong lock. Use a monitor when related data and operations should form one protected abstraction. Use semaphores when independent threads need explicit resource counts or ordering.
Key hazards
Incorrect monitor code can still cause:
- Deadlock: threads wait forever for locks or conditions.
- Starvation: a thread is repeatedly denied entry or never signalled.
- Lost progress: code signals the wrong condition or fails to signal after changing shared state.
- Invariant violations: code changes monitor data without holding the monitor lock.
Exercise
Why must a condition-variable wait normally appear inside a while loop?
Show answer
Because another thread may change the shared state before the awakened thread runs, and some systems permit spurious wake-ups. The loop checks that the condition is still true.