8
Monitors and Blocking Synchronization
8.1 Introduction
Monitors are a structured way of combining synchronization and data. A class encapsulates both data and methods in the same way that a monitor combines data, methods, and synchronization in a single modular package.
Here is why modular synchronization is important. Let us imagine our application has two threads, a producer and a consumer, that communicate through a shared FIFO queue. We could have the threads share two objects: an unsynchronized queue, and a lock to protect the queue. The producer looks something like this:
mutex.lock();
try {
queue.enq(x)
} finally {
mutex.unlock();
}
This is no way to run a railroad. Suppose the queue is bounded, meaning that an attempt to add an item ...
Get The Art of Multiprocessor Programming, Revised Reprint now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.