The producer-consumer pattern allows a producer thread to generate data (e.g loaded from disk or read from a web site) and put in a queue, concurrently with a thread that consumes the data from the queue.
A typical application of this is the processing of multiple disk files. When done in a single threaded, sequential manner, the CPU mostly sits idle while waiting for the disk during file load. The disk, in turn, doesn’t do anything useful while waiting for the CPU to finish processing a file before the next one is loaded. Using the producer-consumer pattern, the files are loaded from the disk and queued while the CPU processes files already in the queue, speeding up the processing of the second and all subsequent files. To boot, you get the possibility of running multiple producers and multiple consumers concurrently, as well as the smug feeling that goes with it. Read more