#include <circular_buffer.hpp>
template<typename T>
circular_buffer class
A generic circular buffer implementation. Similar to the job queue, it can be used to implement the consumer-producer pattern, but this implementation is cache-friendlier since we use a contiguous array as our data store.
| Template parameters | |
|---|---|
| T | type of entry that is transferred between the consumers and producers. |
Example
#include <circular_buffer.hpp> // instantiate a buffer mt::circular_buffer<char> buffer(100); // put some characters into it for (int i = 0; i < 100; ++i) { buffer.add_entry(static_cast<char>(97 + i % 26)); } // buffer is already full so this will block buffer.add_entry('a'); // in some other thread while ((auto c = buffer.get_entry())) { // do something useful with c } // similar interface as job_queue: mark the buffer as inactive buffer.stop_buffer();
Constructors, destructors, conversion operators
- circular_buffer(size_t cap)
- Construct a new circular buffer object.
Public functions
- auto add_entry(const T& entry) -> bool -> auto
- Add a new entry to the buffer.
- auto get_entry() -> T -> auto
- Get an entry from the buffer.
- void stop_buffer()
- Mark the buffer as inactive and notify all waiters.
- auto size() const -> size_t -> auto
- Return the number of entries in the buffer.
- auto cap() const -> size_t -> auto
- Return the capacity of the buffer.
Function documentation
template<typename T>
mt:: circular_buffer<T>:: circular_buffer(size_t cap)
Construct a new circular buffer object.
| Parameters | |
|---|---|
| cap | capacity of the circular buffer |
template<typename T>
auto mt:: circular_buffer<T>:: add_entry(const T& entry) -> bool
Add a new entry to the buffer.
| Parameters | |
|---|---|
| entry | new entry to be placed into the buffer |
| Returns | true if entry was successfully added to the buffer |
template<typename T>
auto mt:: circular_buffer<T>:: get_entry() -> T
Get an entry from the buffer.
| Returns | T is the entry returned. It's empty if failed to get entry |
|---|
template<typename T>
auto mt:: circular_buffer<T>:: size() const -> size_t
Return the number of entries in the buffer.
| Returns | size_t, the size of the queue |
|---|
template<typename T>
auto mt:: circular_buffer<T>:: cap() const -> size_t
Return the capacity of the buffer.
| Returns | size_t, the capacity of the buffer |
|---|