This question evaluates understanding of ring-buffer data structures, circular indexing, fixed-capacity storage, and object-oriented design for a single-producer, multi-consumer implementation in C++.
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:
After coding, briefly explain how you would adapt the design if true thread safety were required.