All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Radiant::BlockRingBuffer< T > Class Template Reference

Lock-free and thread-safe ring buffer with one producer and one consumer. More...

#include <Radiant/BlockRingBuffer.hpp>

Classes

class  Reader
 Helper class for reading continuous memory segments from the buffer The data can be used freely while this object is alive, it's actually consumed in the destructor. More...
 
class  Writer
 

Public Member Functions

 BlockRingBuffer (int capacity)
 Creates a new buffer with given capacity.
 
 BlockRingBuffer (const BlockRingBuffer &o)
 Copies the object, not thread-safe.
 
void consume (int count)
 Consumes exactly count elements from the buffer. More...
 
BlockRingBufferoperator= (const BlockRingBuffer &o)
 Copies the object, not thread-safe.
 
void produce (int count)
 Consumes exactly count elements from the buffer. More...
 
int read (T *output, int count)
 Consumes max count elements from the buffer and writes them to output. More...
 
Reader read (int count)
 Returns a reader object that has continuous memory segment up to count bytes, but it can be less if there isn't enough continuous data available.
 
int size () const
 Returns the number of elements in the buffer. Can be called from all threads.
 
int write (const T *input, int count)
 Write up to count elements from data to the buffer. More...
 
Writer write (int count)
 

Detailed Description

template<typename T>
class Radiant::BlockRingBuffer< T >

Lock-free and thread-safe ring buffer with one producer and one consumer.

The API is optimized for storing bigger blocks (like float arrays) to the buffer, but it works with all elements that can be copied and default-constructed.

Member Function Documentation

template<typename T >
void Radiant::BlockRingBuffer< T >::consume ( int  count)
inline

Consumes exactly count elements from the buffer.

Can be called only from the reader thread.

Parameters
countnumber of elements to consume, must not be bigger than size().
template<typename T >
void Radiant::BlockRingBuffer< T >::produce ( int  count)
inline

Consumes exactly count elements from the buffer.

Can be called only from the reader thread.

Parameters
countnumber of elements to consume, must not be bigger than available buffer size().
template<typename T >
int Radiant::BlockRingBuffer< T >::read ( T *  output,
int  count 
)
inline

Consumes max count elements from the buffer and writes them to output.

Returns
number of elements consumed and written to output, less than count if the buffer doesn't have enough data
template<typename T >
int Radiant::BlockRingBuffer< T >::write ( const T *  input,
int  count 
)
inline

Write up to count elements from data to the buffer.

Returns
number of elements written, less than count if the buffer fills up