Monitor¶
Category: Synchronization · Status: stub · Lessons: chapter 04, Waiting for each other (planned)
One line: An object whose methods all run under one built-in lock, with condition variables for waiting inside it — Java's synchronized with wait and notify.
Also called: synchronized, intrinsic lock.
How it connects¶
flowchart LR
n_condition_variable["Condition variable"]
n_monitor["Monitor"]
n_mutex["Mutex"]
n_synchronization["Synchronization"]
n_monitor -->|is a| n_synchronization
n_monitor -->|uses| n_condition_variable
n_monitor -->|uses| n_mutex
classDef center stroke-width:3px
class n_monitor center
classDef outside stroke-dasharray: 4 3
class n_condition_variable,n_mutex,n_synchronization outside
- Is a kind of: Synchronization
- Is built on: Condition variable, Mutex
In each language¶
| Rust | No monitor type: a Mutex<T> ↗ holds the data and a Condvar ↗ waits on its guard |
| Go | No monitor: a sync.Cond ↗ carries a Locker that must be held when the condition changes and when calling Wait |
| Java | Every object has one: JLS §17.1 ↗ synchronized locks it, and Object.wait ↗, notify and notifyAll wait inside it |
| Python | threading.Condition ↗ bundles a lock with wait and notify |
| C# | Monitor ↗: Enter and Exit, and Wait, Pulse and PulseAll inside; the lock statement uses it |
| Kotlin | @Synchronized ↗ marks the generated JVM method as synchronized |
Where to read more¶
- In the books: The Art of Multiprocessor Programming, Maurice Herlihy, Nir Shavit — ch. 8, 'Monitors and Blocking Synchronization'
- In the books: Modern Multithreading, Richard H. Carver, Kuo-Chung Tai — ch. 4, 'Monitors'
- In the books: Concurrency with Modern C++, Rainer Grimm — ch. 9, 'Concurrent Architecture' → 'Monitor Object'
- In the books: Learning Concurrent Programming in Scala, Aleksandar Prokopec — ch. 2, 'Concurrency on the JVM and the Java Memory Model' → 'Monitors and synchronization'
- In the books: Pro Asynchronous Programming with .NET, Richard Blewett, Andrew Clymer — ch. 4, 'Basic Thread Safety' → 'Monitor: The Workhorse of .NET Synchronization'
- In the books: Concurrent Programming: Algorithms, Principles, and Foundations, Michel Raynal — ch. 3, 'Lock-Based Concurrent Objects' → 'A Construct for Imperative Languages: the Monitor'
- Notes: synchronized constructs ↗
- Reference: Wikipedia: Monitor (synchronization) ↗