AlgoViz

Monitor

medium 50 min

A monitor puts shared data, the lock that guards it and the conditions threads wait for into one object.

Watch lesson4:27

Where it shows up

The same pattern appears across everyday software.

  • Java objects

    The Java Language Specification says that each object is associated with a monitor, and defines wait sets and notification. Object.wait documents that waiting should happen in a loop because of spurious wakeups.

  • Java Condition

    java.util.concurrent.locks.Condition gives one lock several wait sets. Its documentation advises to always assume that spurious wakeups can occur.

  • ArrayBlockingQueue

    Its source says that concurrency control uses the classic two-condition algorithm found in any textbook: one ReentrantLock, notEmpty and notFull.

  • Python queue.Queue

    Its source makes one mutex and three conditions on it: not_empty, not_full and all_tasks_done.

  • C# Monitor

    .NET has a class called Monitor. Its documentation describes the lock holder, a ready queue and a waiting queue for each synchronized object.

  • POSIX threads

    The standard for pthread_cond_wait states that spurious wakeups may occur, so the predicate must be checked again on return.

  • Ruby Monitor

    Ruby's standard library has Monitor and MonitorMixin, described as an object or module whose methods are executed with mutual exclusion.

  • Guava Monitor

    A class that replaces ReentrantLock: threads wait for Guards, which are boolean conditions, and signalling is implicit.

  • Ada protected objects

    A language construct: protected actions exclude each other, and calls to an entry queue until its barrier is open. The entry queues are served after a change, without an explicit signal.

  • Go sync.Cond

    A condition variable on a Locker. Its documentation says that for many simple uses channels are the better choice.