Process synchronization mutual exclusion tool: monitor

1. What is a monitoring

Monitors refer to the management of shared variables and the process of operating shared variables to allow them to support concurrency. It is the only concurrency primitive provided by the Java language before 1.5, and the SDK concurrency package provided after Java 1.5 is also based on monitor technology.

2. Why use monitoring

The semaphore mechanism itself has shortcomings: the process has its own synchronization operation, and a large number of P(S) and V(S) operations are scattered in each process, which is not easy to manage and is prone to deadlock. The monitor encapsulates the synchronization operation, conceals the synchronization details of the process, simplifies the call interface of the synchronization function, and makes it as simple as writing a sequential (serial) program for the user to write a concurrent program.

The purpose of introducing the management mechanism:

  • Centralize the critical areas scattered in each process for management;
  • Prevent the process from intentionally or unintentionally illegal synchronization operation;
  • It is convenient to write the program in high-level language, and it is also convenient to verify the correctness of the program.

3. How does the pipeline solve the concurrency problem

There are two core issues in the field of concurrent programming: one is mutual exclusion, that is, only one thread is allowed to access shared resources at a time; the other is synchronization, that is, how to communicate and collaborate between threads. Both of these problems can be solved by monitoring.

Today we focus on the most widely used management model: MESA model .

1. The solution of the mutual exclusion problem by the monitor

Insert picture description here
As shown in FIG:

  • Monitor X encapsulates the shared variable queue and enq() and deq() operations of enqueue ;
  • If thread A and thread B want to access the shared variable queue , they can only do so by calling the enq() and deq() methods provided by the monitor ;
  • enq() and deq() are mutually exclusive, allowing only one thread to enter the monitor .

2. The solution of the synchronization problem by the monitor

Insert picture description here
The outermost box represents the package. When multiple threads try to enter the monitor at the same time, only one thread is allowed to enter, and other threads need to wait in the entry waiting queue .

The concept of condition variables is introduced in the management process, and each condition variable corresponds to a waiting queue. For example, the condition variable A and condition variable B in the above figure each have their own waiting queue.

  • If there is a thread T1 that needs to perform the dequeue operation, it can only be successfully executed when the queue is not empty. At this time, the precondition that the queue is not empty is the condition variable in the monitor.

  • If thread T1 finds that the queue is empty after entering, it needs to wait in the waiting queue of the condition variable " Queue is not empty ".

  • When another thread T2 executes the enqueue operation and the operation is successfully executed, the condition of " queue is not empty " has been satisfied for thread T1. At this time, thread T2 must notify T1 that the required conditions have been satisfied.

  • When the thread T1 is notified, it will come out of the waiting queue, but it will not be executed immediately after it comes out, but will re-enter the entry waiting queue to wait.

Among them, T1 goes to wait in the waiting queue of the condition variable, which is realized by calling wait() ;

Thread T2 notifies T1 that "the queue is not empty" by calling notify() .

The difference between notify() and notifyAll() is that notify() can notify a thread in the waiting queue, while notifyAll() can notify all threads in the waiting queue.

Points to note:

1. For the MESA monitor, there is a programming paradigm, which is to call wait() in a while loop . This is unique to the MESA pipeline.

while(条件不满足) {
    
    
 wait();
}

2. Try to use notifyAll() instead of notify() . Only when the following three conditions are met, notify() can be used :

  • All waiting threads have the same waiting conditions;
  • After all waiting threads are awakened, perform the same operation;
  • Only need to wake up one thread.

4. Limitations of Management

Most commonly used programming languages ​​do not implement monitors. If a certain language does not support monitors, it is very difficult to join monitors.

Guess you like

Origin blog.csdn.net/j1231230/article/details/113883593