#pragma once #include #include #include #include template class RingBuffer { static_assert(N > 1, "Buffer size must be greater than 1"); private: std::vector data; size_t read_ptr = 0; size_t write_ptr = 0; bool full = false; std::mutex mtx; std::condition_variable not_empty; size_t advance(size_t ptr) const { return (ptr + 1) % N; } public: RingBuffer() : data(N, 0) {} void push(int value) { std::unique_lock lock(mtx); data[write_ptr] = value; if(full) { read_ptr = advance(read_ptr); } write_ptr = advance(write_ptr); full = (write_ptr == read_ptr); not_empty.notify_one(); } int pop() { std::unique_lock lock(mtx); not_empty.wait(lock, [this]() { return !is_empty(); }); int value = data[read_ptr]; read_ptr = advance(read_ptr); full = false; return value; } bool is_empty() const { return !full && (read_ptr == write_ptr); } bool is_full() const { return full; } };