Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
roc::core::SpscRingBuffer< T > Class Template Reference

Thread-safe lock-free single-producer single-consumer circular buffer of copyable objects. More...

#include <spsc_ring_buffer.h>

Inheritance diagram for roc::core::SpscRingBuffer< T >:
Collaboration diagram for roc::core::SpscRingBuffer< T >:

Public Member Functions

 SpscRingBuffer (IArena &arena, size_t n_elements)
 Initialize. More...
 
 ~SpscRingBuffer ()
 Deinitialize. More...
 
bool is_valid () const
 Check that allocation succeeded. More...
 
bool is_empty () const
 Check if buffer is empty. More...
 
bool push_back (const T &element)
 Append element to the end of the buffer. If buffer is full, drops element and returns false. Should be called from writer thread. Lock-free. More...
 
bool pop_front (T &element)
 Fetch element from the beginning of the buffer. If buffer is empty, returns false. Should be called from reader thread. Lock-free. More...
 

Detailed Description

template<class T>
class roc::core::SpscRingBuffer< T >

Thread-safe lock-free single-producer single-consumer circular buffer of copyable objects.

Allows access from two concurrent threads: writer and reader. Both writer and reader are never blocked. Provides sequential consistency.

Template Parameters
Tdefines object type, it should be copyable.

Implemented on top of SpscByteBuffer.

Definition at line 33 of file spsc_ring_buffer.h.

Constructor & Destructor Documentation

◆ SpscRingBuffer()

template<class T >
roc::core::SpscRingBuffer< T >::SpscRingBuffer ( IArena arena,
size_t  n_elements 
)
inline

Initialize.

Definition at line 36 of file spsc_ring_buffer.h.

◆ ~SpscRingBuffer()

template<class T >
roc::core::SpscRingBuffer< T >::~SpscRingBuffer ( )
inline

Deinitialize.

Definition at line 41 of file spsc_ring_buffer.h.

Member Function Documentation

◆ is_empty()

template<class T >
bool roc::core::SpscRingBuffer< T >::is_empty ( ) const
inline

Check if buffer is empty.

Definition at line 54 of file spsc_ring_buffer.h.

◆ is_valid()

template<class T >
bool roc::core::SpscRingBuffer< T >::is_valid ( ) const
inline

Check that allocation succeeded.

Definition at line 49 of file spsc_ring_buffer.h.

◆ pop_front()

template<class T >
bool roc::core::SpscRingBuffer< T >::pop_front ( T &  element)
inline

Fetch element from the beginning of the buffer. If buffer is empty, returns false. Should be called from reader thread. Lock-free.

Definition at line 79 of file spsc_ring_buffer.h.

◆ push_back()

template<class T >
bool roc::core::SpscRingBuffer< T >::push_back ( const T &  element)
inline

Append element to the end of the buffer. If buffer is full, drops element and returns false. Should be called from writer thread. Lock-free.

Definition at line 62 of file spsc_ring_buffer.h.


The documentation for this class was generated from the following file: