BS_Praktikum4/ring_buffer.h
2025-06-03 01:49:23 +02:00

63 lines
1.3 KiB
C++

#pragma once
#include <vector>
#include <cstddef>
#include <mutex>
#include <condition_variable>
template <size_t N>
class RingBuffer {
static_assert(N > 1, "Buffer size must be greater than 1");
private:
std::vector<int> 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<std::mutex> 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<std::mutex> 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;
}
};