Bounded Blocking Buffer with Shutdown and Timeouts
You are asked to design and implement a thread-safe, fixed-capacity producer–consumer buffer that supports multiple producers and consumers.
Functional Requirements
-
FIFO ordering of items.
-
Configurable capacity N.
-
put(item):
-
Blocks when the buffer is full.
-
A timed put(item, timeout) variant that times out if space does not become available in time.
-
take():
-
Blocks when the buffer is empty.
-
A timed take(timeout) variant that times out if no item becomes available in time.
-
shutdown():
-
Unblocks any waiting producers/consumers.
-
Rejects new puts after shutdown is invoked.
-
After shutdown, consumers may still take remaining items; if the buffer is empty, take should not block.
Non-Functional and Correctness Requirements
-
Thread-safe for multiple producers and consumers.
-
Prevent race conditions, deadlocks, and lost wakeups.
-
Discuss fairness and performance trade-offs.
-
Analyze time and space complexity.
-
If time permits: compare a lock-based design (mutex + condition variables) to a lock-free approach.
Assume a Java-like API and execution model (mutex/condition variables, interrupts, and timed waits). You may use a circular array for FIFO ordering.