One object owns the rules
A monitor wraps shared state inside an object so every method automatically holds the mutex while it runs. Callers never touch the lock directly; the monitor enforces mutual exclusion for them.
The building blocks
- A mutex that every public method acquires on entry and releases on exit.
- One or more condition variables for threads that must wait inside a method.
- Invariants that hold whenever no thread is mid method.
A thread that cannot proceed, like a consumer facing an empty queue, waits on a condition variable. Waiting releases the monitor lock so producers can enter and refill.
Why bundle it
Scattering lock and unlock calls across a codebase invites mistakes: a missed unlock, a wrong order, a forgotten guard. A monitor centralizes that discipline in one place, making the shared resource safe by construction.
Key idea
A monitor binds shared data to a mutex and condition variables so every method runs under exclusion automatically, turning correct locking into a property of the object rather than the caller.