Producer Consumer issues , also known as the limited buffer problem , is a classic case of multithreading synchronization problem.
The problem describes two threads that share fixed-size buffers-the so-called "producer" and "consumer"-problems that can occur when they actually run. The primary role of the producer is to generate a certain amount of data into the buffer, and then repeat the process.
At the same time, consumers consume the data in buffers. The key to this issue is to ensure that producers do not add data when the buffer is full, and consumers do not consume data when the buffer is empty.
To solve this problem, you must let the producer hibernate when the buffer is full (or simply discard the data), and wait until the next time the consumer consumes the data in the buffer, the producer can wake up and start adding data to the buffer. Similarly, you can let consumers hibernate when the buffer is empty, wait until the producer adds data to the buffer, and then wake the consumer.
The method of inter-process communication is usually used to solve the problem, and the common method is the signal light method [1]. If the workaround is not perfect, the deadlock situation is prone to occur. When a deadlock occurs, two threads fall into hibernation, waiting for the other person to wake up. The problem can also be extended to multiple producers and consumers.
Signal method
The semaphore can avoid the situation where the above wake-up instruction does not work. The method uses two beacons, Fillcount and Emptycount. Fillcount is used to record the number of data items that will be read in the buffer (in fact, how many data items are in the buffer), andEmptycount is used to record the number of free space in the buffer . When new data items are put into the buffer, fillcount increases and emptycount decreases. If a producer tries to reduce emptycount by discovering that its value is zero, the producer goes to sleep . When data items are consumed and emptycount increases, the producer is awakened. Consumer behavior is similar.
Solve problems when there is only one producer and one consumer.
In the case of multiple producers or multiple consumers sharing buffers, the algorithm also results in a race condition where two or more threads are reading or writing the same buffer slot at the same time. To illustrate how this happens, you can assume a possible implementation of Putitemintobuffer () by looking for the next available empty slot and then writing the data item. The following scenarios may occur:
- Two producers have reduced the value of emptycount;
- A producer finds the next available empty slot;
- Another producer also found the next available empty slot, and the result was the same empty slot that was found in the previous step;
- Two producers write data to a usable empty slot.
In order to solve this problem, it is necessary to ensure that only one producer can execute Putitemintobuffer () at the same time. That is, you need to find a way to implement the Code of the critical section mutually exclusive . To achieve this, a two-value semaphore mutex can be introduced with a value of only 1 or 0. If you put a thread between a down (mutex) and an up (mutex), you can restrict that only one thread can be executed.
Producer-Consumer issues