Producer/consumer tasks, threads, and processes are common in operating systems. Applications of the producer/consumer problem, also known as the "bounded buffer problem," range from compilers producing assembly code consumed by assemblers, to the pi pe command implemented in the UNIX shell. In this article, I present a C++ template implementation of a bounded buffer as a circular queue. I also show how to make, the circular queue compatible with STL algorithms by providing a special iterator used as an interface between the algorithms and the circular queue. The bounded buffer problem requires: 1. Mutually exclusive access by the producer and consumer to the buffer. 2. Synchronization between the producer and consumer when the buffer is full or empty.
展开▼