Producer-Consumer Bounded Queue
Problem Implement a thread-safe bounded queue (blocking buffer) shared by producer and consumer threads. Producers call put(item); consumers call take(). When the queue is full, put must block until space is available; when empty, take must block until an item arrives. No busy-waiting.
Input / Output
- Input: a fixed capacity, and concurrent put/take calls from many threads.
- Output: each item produced is consumed exactly once, in FIFO order, with no lost or duplicated items.
Constraints
- Bounded capacity — put blocks on full, take blocks on empty.
- Must be free of data races and deadlocks under many concurrent producers and consumers.
- No busy-wait / spin polling.
Example
- capacity 2; producers put a, b, c while a consumer takes: the third put blocks until the take frees a slot. Items come out a, b, c in order.
added …