Implement a fixed-capacity ring buffer in C++ for a setting with one producer and multiple consumers.
Design a class that stores integers in a preallocated circular array and supports the following operations:
-
RingBuffer(int capacity)
: initialize the buffer with a fixed capacity.
-
bool push(int value)
: insert a value at the tail. Return
false
if the buffer is full.
-
bool pop(int& value)
: remove the oldest value from the head and write it into
value
. Return
false
if the buffer is empty.
-
bool empty() const
-
bool full() const
-
int size() const
Requirements:
-
All operations should run in
O(1)
time.
-
Use circular indexing to reuse freed space.
-
Do not resize the underlying storage.
-
Focus on the
object-oriented design
and correct ring-buffer behavior.
-
You may assume synchronization is handled externally; the main goal is to implement the ring-buffer logic correctly for a single-producer, multi-consumer use case.
After coding, briefly explain how you would adapt the design if true thread safety were required.