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

Single producer / single consumer wait-free ring buffer. More...

#include <spsc_ring_buffer.h>

Public Member Functions

 SPSCRingBuffer (size_t capacity)
 Default class constructor. More...
 
 SPSCRingBuffer (const SPSCRingBuffer &)=delete
 
 SPSCRingBuffer (SPSCRingBuffer &&)=delete
 
 ~SPSCRingBuffer ()
 
SPSCRingBufferoperator= (const SPSCRingBuffer &)=delete
 
SPSCRingBufferoperator= (SPSCRingBuffer &&)=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 in bytes. More...
 
size_t size () const noexcept
 Get ring buffer size in bytes. 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

Single producer / single consumer wait-free ring buffer.

Single producer / single consumer wait-free ring buffer use only atomic operations to provide thread-safe enqueue and dequeue operations. Ring buffer is bounded to the fixed capacity provided in the constructor.

FIFO order is guaranteed!

Thread-safe.

A combination of the algorithms described by the circular buffers documentation found in the Linux kernel, and the bounded MPMC queue by Dmitry Vyukov. Implemented in pure C++11. Should work across most CPU architectures. http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue

Examples
threads_spsc_ring_buffer.cpp.

Definition at line 32 of file spsc_ring_buffer.h.

Constructor & Destructor Documentation

◆ SPSCRingBuffer() [1/3]

CppCommon::SPSCRingBuffer::SPSCRingBuffer ( size_t  capacity)
inlineexplicit

Default class constructor.

Parameters
capacity- Ring buffer capacity (must be a power of two)

Definition at line 11 of file spsc_ring_buffer.inl.

◆ SPSCRingBuffer() [2/3]

CppCommon::SPSCRingBuffer::SPSCRingBuffer ( const SPSCRingBuffer )
delete

◆ SPSCRingBuffer() [3/3]

CppCommon::SPSCRingBuffer::SPSCRingBuffer ( SPSCRingBuffer &&  )
delete

◆ ~SPSCRingBuffer()

CppCommon::SPSCRingBuffer::~SPSCRingBuffer ( )
inline

Definition at line 42 of file spsc_ring_buffer.h.

Member Function Documentation

◆ capacity()

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

Get ring buffer capacity in bytes.

Definition at line 53 of file spsc_ring_buffer.h.

◆ Dequeue()

bool CppCommon::SPSCRingBuffer::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_spsc_ring_buffer.cpp.

Definition at line 65 of file spsc_ring_buffer.inl.

◆ empty()

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

Is ring buffer empty?

Definition at line 51 of file spsc_ring_buffer.h.

◆ Enqueue()

bool CppCommon::SPSCRingBuffer::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_spsc_ring_buffer.cpp.

Definition at line 30 of file spsc_ring_buffer.inl.

◆ operator bool()

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

Check if the buffer is not empty.

Definition at line 48 of file spsc_ring_buffer.h.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ size()

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

Get ring buffer size in bytes.

Definition at line 22 of file spsc_ring_buffer.inl.


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