CppCommon  1.0.4.1
C++ Common Library
Classes | Public Member Functions | List of all members
CppCommon::MPSCRingBuffer Class Reference

Multiple producers / single consumer wait-free ring buffer. More...

#include <mpsc_ring_buffer.h>

Public Member Functions

 MPSCRingBuffer (size_t capacity, size_t concurrency=std::thread::hardware_concurrency())
 Default class constructor. More...
 
 MPSCRingBuffer (const MPSCRingBuffer &)=delete
 
 MPSCRingBuffer (MPSCRingBuffer &&)=delete
 
 ~MPSCRingBuffer ()=default
 
MPSCRingBufferoperator= (const MPSCRingBuffer &)=delete
 
MPSCRingBufferoperator= (MPSCRingBuffer &&)=delete
 
 operator bool () const noexcept
 Check if the buffer is not empty. More...
 
bool empty () const noexcept
 Is ring buffer empty? More...
 
size_t capacity () const noexcept
 Get ring buffer capacity. More...
 
size_t concurrency () const noexcept
 Get ring buffer concurrency. More...
 
size_t size () const noexcept
 Get ring buffer size. More...
 
bool Enqueue (const void *data, size_t size)
 Enqueue a data into the ring buffer (single producer thread method) More...
 
bool Dequeue (void *data, size_t &size)
 Dequeue a data from the ring buffer (single consumer thread method) More...
 

Detailed Description

Multiple producers / single consumer wait-free ring buffer.

Multiple producers / single consumer wait-free ring buffer use only atomic operations to provide thread-safe enqueue and dequeue operations. This data structure consist of several SPSC ring buffers which count is provided as a hardware concurrency in the constructor. All of them are randomly accessed with a RDTS distribution index. All the items available in sesequential or batch mode. All ring buffer sizes are limited to the capacity provided in the constructor.

FIFO order is not guaranteed!

Thread-safe.

Examples
threads_mpsc_ring_buffer.cpp.

Definition at line 35 of file mpsc_ring_buffer.h.

Constructor & Destructor Documentation

◆ MPSCRingBuffer() [1/3]

CppCommon::MPSCRingBuffer::MPSCRingBuffer ( size_t  capacity,
size_t  concurrency = std::thread::hardware_concurrency() 
)
inlineexplicit

Default class constructor.

Parameters
capacity- Ring buffer capacity (must be a power of two)
concurrency- Hardware concurrency (default is std::thread::hardware_concurrency)

Definition at line 11 of file mpsc_ring_buffer.inl.

◆ MPSCRingBuffer() [2/3]

CppCommon::MPSCRingBuffer::MPSCRingBuffer ( const MPSCRingBuffer )
delete

◆ MPSCRingBuffer() [3/3]

CppCommon::MPSCRingBuffer::MPSCRingBuffer ( MPSCRingBuffer &&  )
delete

◆ ~MPSCRingBuffer()

CppCommon::MPSCRingBuffer::~MPSCRingBuffer ( )
default

Member Function Documentation

◆ capacity()

size_t CppCommon::MPSCRingBuffer::capacity ( ) const
inlinenoexcept

Get ring buffer capacity.

Definition at line 57 of file mpsc_ring_buffer.h.

◆ concurrency()

size_t CppCommon::MPSCRingBuffer::concurrency ( ) const
inlinenoexcept

Get ring buffer concurrency.

Definition at line 59 of file mpsc_ring_buffer.h.

◆ Dequeue()

bool CppCommon::MPSCRingBuffer::Dequeue ( void *  data,
size_t &  size 
)
inline

Dequeue a data from the ring buffer (single consumer thread method)

The data will be copied from the ring buffer using 'memcpy()' function.

Will not block.

Parameters
data- Data buffer to dequeue
size- Data buffer size
Returns
'true' if the data was successfully dequeue, 'false' if the ring buffer is empty
Examples
threads_mpsc_ring_buffer.cpp.

Definition at line 38 of file mpsc_ring_buffer.inl.

◆ empty()

bool CppCommon::MPSCRingBuffer::empty ( ) const
inlinenoexcept

Is ring buffer empty?

Definition at line 55 of file mpsc_ring_buffer.h.

◆ Enqueue()

bool CppCommon::MPSCRingBuffer::Enqueue ( const void *  data,
size_t  size 
)
inline

Enqueue a data into the ring buffer (single producer thread method)

The data will be copied into the ring buffer using 'memcpy()' function. Data size should not be greater than ring buffer capacity!

Will not block.

Parameters
data- Data buffer to enqueue
size- Data buffer size
Returns
'true' if the data was successfully enqueue, 'false' if the ring buffer is full
Examples
threads_mpsc_ring_buffer.cpp.

Definition at line 26 of file mpsc_ring_buffer.inl.

◆ operator bool()

CppCommon::MPSCRingBuffer::operator bool ( ) const
inlineexplicitnoexcept

Check if the buffer is not empty.

Definition at line 52 of file mpsc_ring_buffer.h.

◆ operator=() [1/2]

MPSCRingBuffer& CppCommon::MPSCRingBuffer::operator= ( const MPSCRingBuffer )
delete

◆ operator=() [2/2]

MPSCRingBuffer& CppCommon::MPSCRingBuffer::operator= ( MPSCRingBuffer &&  )
delete

◆ size()

size_t CppCommon::MPSCRingBuffer::size ( ) const
inlinenoexcept

Get ring buffer size.

Definition at line 18 of file mpsc_ring_buffer.inl.


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