libboxes
boxes is a set of specialised containers built on top of STL
ring_buffer_example.cpp
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
int main(int, const char *[]) {
std::mutex m;
std::condition_variable cv;
// fixed-size ring buffer
std::thread producer([&] {
for (int i = 0; i < 32; ++i) {
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, [&buf] { return !buf.full(); });
if (!buf.push_back(i)) {
std::cerr << "Failed to push_back" << std::endl;
}
cv.notify_one();
}
});
std::thread consumer([&] {
for (int i = 0; i < 32; ++i) {
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, [&] { return !buf.empty(); });
std::cout << buf.front() << std::endl;
if (!buf.pop_front()) {
std::cerr << "Failed to pop_front" << std::endl;
}
cv.notify_one();
}
});
producer.join();
consumer.join();
return 0;
}
Implements a fixed-size double ended queue.
Definition: ring_buffer.hpp:31
bool pop_front()
Removes the first element from the RingBuffer.
bool push_back(Arg &&value)
Inserts a new element at the end of the RingBuffer.
bool full() const BOXES_NOTHROW
Returns true if the RingBuffer is full.
T & front()
Returns the first element in the queue.
bool empty() const BOXES_NOTHROW
Returns true if the RingBuffer is empty.
Implements a fixed-size double-ended queue.