Nepedia
Back to Discover
Advanced Reader
💻

Monitor (synchronization)

technology Maturity 11-13 war conflict Next article →

This article covers sensitive topics: war_conflict. Parents can manage visibility in Parental Controls.

{

"text": "In concurrent programming, a monitor is a synchronization construct. It manages how multiple threads access a shared object's state. Without this control, threads might access data at the same time. This can cause data races or logical errors. A monitor ensures that only one thread uses the object at a time. It also allows threads to wait for specific conditions to be met. This makes monitors a vital tool for building thread-safe objects, classes, or modules.

Monitor (synchronization)-SU
Monitor (synchronization)-SU

\n\nThe core of a monitor is mutual exclusion. This means at most one thread can execute a monitor's methods at any time. To enforce this, a monitor uses a mutex, which is a type of lock. When a thread calls a public method, it must first acquire the mutex. This is often described as the thread \"occupying\" the object. If another thread is already using the object, the new thread must wait. Once the first thread finishes its method, it releases the lock. This allows the next waiting thread to enter and begin its task. Without this strict locking, two threads could interfere with each other. For instance, two threads might both see a bank balance of 1000. Both could withdraw 1000 at once, but the balance might only drop by 1000. This creates a serious error in the system's data.\n\nSometimes, mutual exclusion alone is not enough for a program to work. A thread might enter the monitor but find it cannot proceed. For example, a thread might need to remove an item from a list. If the list is currently empty, the thread cannot do its job. A simple solution is \"busy waiting,\" where a thread loops constantly to check the state. However, this wastes a lot of CPU resources. Another option is to unlock the mutex, wait a set amount of time, and then re-lock it. This is difficult because choosing the right wait time is hard. If the time is too short, the CPU is wasted. If it is too long, the program becomes unresponsive.

Monitor (synchronization)-Mesa
Monitor (synchronization)-Mesa

\n\nTo solve these issues, monitors use condition variables. A condition variable is a queue of threads waiting for a specific state change. It is associated with the monitor's mutex. When a thread needs a condition to be true, it performs a \"wait\" operation. This operation is very specific and must be done atomically. Atomically means the steps happen as one single, uninterrupted unit. During a wait, the thread releases the mutex and goes to sleep. Because the thread no longer holds the lock, other threads can enter the monitor. This allows those other threads to change the state and make the condition true.

Monitor (synchronization)-Java
Monitor (synchronization)-Java

\n\nOnce the state changes, a thread can use a \"signal\" or \"notify\" operation. This operation tells the condition variable to wake up a waiting thread. The signaled thread moves from the sleep queue to a ready queue. Once it is awake, the thread must automatically re-acquire the mutex. It cannot resume its work until it has exclusive access to the object again. This careful hand-off prevents race conditions. A race condition happens when the timing of events causes an error. One major risk is a \"missed wakeup.\" This could happen if a thread releases a lock but is interrupted before it actually goes to sleep. If a signal happens during that tiny gap, the thread might sleep forever. The atomic nature of the wait operation prevents this mistake.\n\nA famous example of needing a monitor is the bounded producer/consumer problem. In this scenario, "producer" threads create tasks and put them into a queue. "Consumer" threads take those tasks out of the queue to process them. The queue has a maximum size, which is its bound. If the queue is full, producers must stop and wait for room. If the queue is empty, consumers must stop and wait for tasks. Using a monitor ensures that these accesses are atomic. This prevents the queue from entering an inconsistent state. Without synchronization, a consumer might try to take a task from a queue that just became empty. A producer might try to add a task to a queue that just became full. Both would cause errors in the program.\n\nMonitors were invented by computer scientists Per Brinch Hansen and C. A. R. Hoare. Brinch Hansen first implemented them in his Concurrent Pascal programming language. Their work changed how developers manage complex, multi-threaded systems. By combining mutual exclusion with condition variables, monitors provide a complete solution for thread safety. They allow programmers to write code that is both efficient and reliable. This prevents the waste of CPU power seen in spin-waiting. It also ensures that data remains accurate even when many tasks run at once.",

"media": [

"File:Monitor (synchronization)-SU.png",

"File:Monitor (synchronization)-Mesa.png",

"File:Monitor (synchronization)-Java.png"

]

}

813 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (3)
File:Monitor (synchronization)-SU.png
Monitor (synchronization)-SU.png
File:Monitor (synchronization)-Mesa.png
Monitor (synchronization)-Mesa.png
File:Monitor (synchronization)-Java.png
Monitor (synchronization)-Java.png
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.