CircularBuffer
Namespace: UTILSLIB · Library: Utilities Library
#include <utils/circularbuffer.h>
template <typename T>
class UTILSLIB::CircularBuffer
TEMPLATE CIRCULAR BUFFER.
Thread-safe lock-free circular (ring) buffer for producer-consumer data exchange between threads.
CircularBuffer<double> buffer(4); // single producer, single consumer
const double block[3] = {1.0, 2.0, 3.0};
buffer.push(block, 3);
double first = 0.0;
double second = 0.0;
buffer.pop(first);
buffer.pop(second);
Public Methods
CircularBuffer(uiMaxNumElements)
Constructs a CircularBuffer.
Parameters:
- uiMaxNumElements : unsigned int length of buffer.
~CircularBuffer()
Destroys the CircularBuffer.
push(pArray, size)
Adds a whole array at the end buffer.
Parameters:
-
pArray : *const T ** pointer to an Array which should be apend to the end.
-
size : unsigned int number of elements containing the array.
Returns:
- bool — True if all elements were written, false if the buffer is paused or no space freed up within the timeout.
push(newElement)
Adds an element at the end of the buffer.
Parameters:
- newElement : const T & pointer to an Array which should be apend to the end.
Returns:
- bool — True if the element was written, false if the buffer is paused or no space freed up within the timeout.
pop(element)
Returns the first element (first in first out).
Parameters:
- element : T & Receives the first element; left unchanged on failure.
Returns:
- bool — True if an element was popped, false if the buffer is paused or empty within the timeout.
clear()
Clears the buffer.
pause()
Pauses the buffer.
Skpis any incoming matrices and only pops zero matrices.
Parameters:
- bPause True to pause the buffer, false to resume it.
getFreeElementsRead()
Returns the number of free elements for thread safe reading.
Returns:
- int — Number of elements currently available to pop.
getFreeElementsWrite()
Returns the number of free elements for thread safe reading.
Returns:
- int — Number of free slots currently available to push into.
Example
- Full source:
src/examples/ex_utils/main.cpp - Build target:
ex_utils(runs under CTest)
Used in examples
ex_utils
Utilities library: buffers, observers, logging, file formats, montages, layouts and reports.
CircularBuffer<double> buffer(4); // single producer, single consumer
const double block[3] = {1.0, 2.0, 3.0};
buffer.push(block, 3);
double first = 0.0;
double second = 0.0;
buffer.pop(first);
buffer.pop(second);
ok &= expect(first == 1.0 && second == 2.0 && buffer.getFreeElementsRead() == 1, "CircularBuffer pops in FIFO order, one element left");
Authors of this file
- Christoph Dinh <christoph.dinh@mne-cpp.org>
- Lorenz Esch <lorenz.esch@tu-ilmenau.de>
- Juan GPC <jgarciaprieto@mgh.harvard.edu>
- Gabriel Motta <gabrielbenmotta@gmail.com>