Computers use a special rule to work. This rule helps many tasks share one thing. Only one task can use it at a time. This keeps everything safe and correct. It helps computers run well. Do you like how computers work?
Computers often do many tasks at once. 


Computers often do many tasks at the same time. These tasks are called threads. Sometimes, threads must share the same object. This can cause mistakes if they do not work together. A monitor is a tool that keeps sharing safe. 
A monitor uses a mutex. A mutex is a lock. It makes sure only one thread uses the object at a time. This is called mutual exclusion. When a thread starts a task, it locks the mutex. When the task ends, it unlocks it. This stops errors like data races. A data race is a mistake that happens when threads clash.
Sometimes, a thread cannot finish its task yet. It might need to wait for a change. A monitor uses condition variables to help. A condition variable is a way for a thread to wait. 
Instead of checking the object over and over, the thread goes to sleep. This saves power. When the state changes, another thread sends a signal. This signal wakes up the waiting thread. The waiting thread then grabs the lock again to finish its work. 
Computers often run many tasks at once. These tasks are called threads. Sometimes, many threads need to use the same piece of information. If they all try to change it at the exact same time, they can cause big mistakes. These mistakes are called data races or logical errors. A monitor is a special tool that keeps this sharing safe. It acts like a manager for a shared object. 
A monitor works using two main parts. The first part is called a mutex, which is a type of lock. When a thread wants to use the object, it must first lock the mutex. This creates something called mutual exclusion. This rule means only one thread can use the object at any single moment. While one thread is working, all other threads must wait their turn. Once the thread finishes, it unlocks the mutex so others can enter. 
Sometimes, a thread cannot finish its job right away. It might need to wait for something specific to happen first. For example, a thread might need to wait until a list is not empty. Instead of checking the list constantly, which wastes computer power, the thread can sleep. This is where condition variables come in. A condition variable is a way for a thread to wait for a change in state. 
When a thread waits on a condition variable, it does something very clever. It temporarily gives up its lock so other threads can enter the monitor. This allows other threads to change the object and make the condition true. Once the change happens, a thread sends a signal. This signal wakes up the sleeping thread. The sleeping thread then tries to grab the lock again to finish its task.
Scientists Per Brinch Hansen and C. A. R. Hoare invented monitors. Brinch Hansen first used them in a language called Concurrent Pascal. Monitors help solve classic problems like the producer-consumer problem. In that problem, one thread makes tasks and another thread uses them. The monitor ensures the maker does not add to a full list. It also ensures the user does not try to take from an empty list. This keeps the whole system running smoothly without errors.
{
"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. 


🖼️ Images & Media (3)
More to explore
✨ What else?
Related topics you might enjoy
🔬 Go deeper
More advanced topics to explore
🪜 Step back
Simpler topics to build understanding
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.