The producer-consumer problem
In the previous chapter, we implemented a version of the producer-consumer problem using condition variables and mentioned that most of the time, the condition variables can be replaced by channels. The producer-consumer implementations we will work on in this chapter demonstrate this point. Some concurrency problems, such as the producer-consumer problem, are by their nature message-passing problems, and trying to solve them using shared-memory utilities results in unnecessarily complicated and lengthy code.
At the core of the producer-consumer problem is limited intermediate storage. At a high level, the producer-consumer problem contains processes that produce objects at various rates, and consumers that consume those objects at various rates, with limited storage in between the two that is used to store the produced objects until they are consumed. The producer-consumer problem is relevant in any system where a balance between the production of...