Producer consumer is an interesting algorithm. Its existence is mainly for two purposes, the first is to meet the continuous creation of resources by producers; the second is to meet the continuous demand of resources by consumers. Of course, because of the limited space, resources can neither be stored nor acquired indefinitely.
Then, some friends may say, what is the function of such a producer consumer algorithm. Let's see how it works in multithreaded communication? First, we define a data structure,
So, at this time, if we need to send messages to a thread, how to send them is very simple. We can think of it as the operation of a producer.
Now that we talked about sending messages, the thread itself has to process these messages.
(1) producer consumer can only use sempoint as lock
(2) when writing code, you need to judge the order of hFull and hEmpty
(3) it's very important to master the basic algorithm of producer consumer, but more importantly, it's own practice