In the mutual exclusion of the learning process, there is a famous issue: producers - consumer issues. This problem is a collection of standards, famous simultaneous programming issues: a limited buffer and two types of threads, they are producers and consumers, producers putting the product into the buffer, the opposite consumer is from the buffer Take the product in the product. The producer must wait until the buffer has space in the buffer. Consumers must wait until they can continue to read until they have products in the buffer. The main consideration is: buffer or buffer empty and competitive conditions. The following is a producer of competitive conditions - consumer problem examples. #define n 100 / * Number of slots in the buffer * / int count = 0; / * number of items in the buffer * / void product (void) {int item; while (true) {product_item (& it); if Count == n) SLEEP (); Enter_Item (item); count = count 1; if (count == 1) Wakeup (consumer);}}}}}}}} {Int item; while (true) {ix ( Count == 0) SLEEP (); remove_item (& item); count = count-1; if (count == n-1) Wakeup (product); consume_item (item);}} In this instance, first define one The common buffer size is 100, which is a critical resource, and then count is the number of products in the buffer, which is initialized to 0. The Producer function is a producer function, which is a product of producers to produce a product, but there is no operation of the buffer at this time. And if (count == n) Sleep (); is a test statement, if the number of products produced and the buffer size is equal, the producer enters the sleep state. If it is not equal, the product is placed in the buffer and the number of products increases 1. IF (count == 1) Wakeup (consumer); This statement looks very much solution, in fact it means that if the number of products in the last operation is 0, consumers have entered sleep state, and now production Another product is produced, and it is not empty in the buffer. At this time, consumers woke up. The consumer function is also the same, but one is taken, the other is put. We can see that there is a potential competitive condition here, so-called competitive conditions are such a situation: a plurality of threads acts on data to depends on the scheduling order of the thread. Competition conditions will occur when the two threads are competing to access the same data. Due to the cause of the time film, a thread can interrupt other threads at any time, so the data may be destroyed or incorrectly explained.