CppCommon
1.0.4.1
C++ Common Library
|
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 () | |
SPSCRingBuffer & | operator= (const SPSCRingBuffer &)=delete |
SPSCRingBuffer & | operator= (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... | |
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
Definition at line 32 of file spsc_ring_buffer.h.
|
inlineexplicit |
Default class constructor.
capacity | - Ring buffer capacity (must be a power of two) |
Definition at line 11 of file spsc_ring_buffer.inl.
|
delete |
|
delete |
|
inline |
Definition at line 42 of file spsc_ring_buffer.h.
|
inlinenoexcept |
Get ring buffer capacity in bytes.
Definition at line 53 of file spsc_ring_buffer.h.
|
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.
data | - Data buffer to dequeue |
size | - Data buffer size |
Definition at line 65 of file spsc_ring_buffer.inl.
|
inlinenoexcept |
Is ring buffer empty?
Definition at line 51 of file spsc_ring_buffer.h.
|
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.
data | - Data buffer to enqueue |
size | - Data buffer size |
Definition at line 30 of file spsc_ring_buffer.inl.
|
inlineexplicitnoexcept |
Check if the buffer is not empty.
Definition at line 48 of file spsc_ring_buffer.h.
|
delete |
|
delete |
|
inlinenoexcept |
Get ring buffer size in bytes.
Definition at line 22 of file spsc_ring_buffer.inl.