b - 1]. The producer simply puts each new message into B[s mod b], and the consumer takes each message from B[r mod b]. The algorithm is shown below, generalized for infinite k. Producer: L: if (s - r) mod k = b then goto L fi; put message in buffer; s := (s + 1) mod k; goto L; Consumer: L: if (s - r) mod k = 0 then goto L fi; take message from buffer; r := (r + 1) mod k; goto L; The Lamport solution uses busy waiting in the thread instead of waiting in the scheduler. This solution neglects the impact of scheduler thread switch at inconvenient times. If the first thread has read a variable value from memory, the scheduler switches to the second thread that changes the variable value, and the scheduler switches back to the first thread then the first thread uses the old value of the variable, not the current value. Atomic read-modify-write solves this problem. Modern C++ offers atomic variables and operations for multi-thread programming. The following busy waiting C++11 solution for one producer and one consumer uses atomic read-modify-write operations fetch_add and fetch_sub on the atomic variable count. enum {N = 4 }; Message buffer[N]; std::atomic<unsigned> count {0}; void producer() { unsigned tail {0}; for (;;) { Message message = produceMessage(); while (N == count) ; // busy waiting buffer[tail++] = message; tail %= N; count.fetch_add(1, std::memory_order_relaxed); } } void consumer() { unsigned head {0}; for (;;) { while (0 == count) ; // busy waiting Message message = buffer[head++]; head %= N; count.fetch_sub(1, std::memory_order_relaxed); consumeMessage(message); } } int main() { std::thread t1(producer); std::thread t2(consumer); t1.join(); t2.join(); } The circular buffer index variables head and tail are thread-local and therefore not relevant for memory consistency. The variable count controls the busy waiting of the producer and consumer thread. See also Atomic operation Design pattern FIFO Pipeline Channel Implementation in Java: Java Message Service References Further reading Mark Grand Patterns in Java, Volume 1, A Catalog of Reusable Design Patterns Illustrated with UML C/C++ Users Journal (Dr.Dobb's) January 2004, "A C++ Producer-Consumer Concurrency Template Library", by Ted Yuan, is a ready-to-use C++ template library. The small template library source code and examples can be found here Ioan Tinca, The Evolution of the Producer-Consumer Problem in Java Articles with example Java code Concurrency (computer science) Edsger W. Dijkstra Problems in