The Producer-Consumer problem is one of the classic thread-related problems. Simply put, it can be described as follows:
Two threads, one that writes to the shared resource (Producer) and one that reads from the shared resource (Consumer), should be synchronized so that the Consumer does not consume more than what the Producer has produced.
The approach to handling this problem involves use of semaphores or monitors. We have seen how monitors are used to create a shared resource. We will now use
SharedBuffer created in the previous recipe to avoid a race condition in a Producer-Consumer scenario. We will also create a
Producer class to write into it and a
Consumer class to read from it, and use threads ...