CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | List of all members
CppCommon::SPSCRingQueue< T > Class Template Reference

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

#include <spsc_ring_queue.h>

Public Member Functions

 SPSCRingQueue (size_t capacity)
 Default class constructor. More...
 
 SPSCRingQueue (const SPSCRingQueue &)=delete
 
 SPSCRingQueue (SPSCRingQueue &&)=delete
 
 ~SPSCRingQueue ()
 
SPSCRingQueueoperator= (const SPSCRingQueue &)=delete
 
SPSCRingQueueoperator= (SPSCRingQueue &&)=delete
 
 operator bool () const noexcept
 Check if the queue is not empty. More...
 
bool empty () const noexcept
 Is ring queue empty? More...
 
size_t capacity () const noexcept
 Get ring queue capacity. More...
 
size_t size () const noexcept
 Get ring queue size. More...
 
bool Enqueue (const T &item)
 Enqueue an item into the ring queue (single producer thread method) More...
 
bool Enqueue (T &&item)
 Enqueue an item into the ring queue (single producer thread method) More...
 
bool Dequeue (T &item)
 Dequeue an item from the ring queue (single consumer thread method) More...
 

Detailed Description

template<typename T>
class CppCommon::SPSCRingQueue< T >

Single producer / single consumer wait-free ring queue.

Single producer / single consumer wait-free ring queue use only atomic operations to provide thread-safe enqueue and dequeue operations. Ring queue 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_queue.cpp.

Definition at line 34 of file spsc_ring_queue.h.

Constructor & Destructor Documentation

◆ SPSCRingQueue() [1/3]

template<typename T >
CppCommon::SPSCRingQueue< T >::SPSCRingQueue ( size_t  capacity)
inlineexplicit

Default class constructor.

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

Definition at line 12 of file spsc_ring_queue.inl.

◆ SPSCRingQueue() [2/3]

template<typename T >
CppCommon::SPSCRingQueue< T >::SPSCRingQueue ( const SPSCRingQueue< T > &  )
delete

◆ SPSCRingQueue() [3/3]

template<typename T >
CppCommon::SPSCRingQueue< T >::SPSCRingQueue ( SPSCRingQueue< T > &&  )
delete

◆ ~SPSCRingQueue()

template<typename T >
CppCommon::SPSCRingQueue< T >::~SPSCRingQueue ( )
inline

Definition at line 44 of file spsc_ring_queue.h.

Member Function Documentation

◆ capacity()

template<typename T >
size_t CppCommon::SPSCRingQueue< T >::capacity ( ) const
inlinenoexcept

Get ring queue capacity.

Definition at line 55 of file spsc_ring_queue.h.

◆ Dequeue()

template<typename T >
bool CppCommon::SPSCRingQueue< T >::Dequeue ( T &  item)
inline

Dequeue an item from the ring queue (single consumer thread method)

The item will be moved from the ring queue.

Will not block.

Parameters
item- Item to dequeue
Returns
'true' if the item was successfully dequeue, 'false' if the ring queue is empty
Examples
threads_spsc_ring_queue.cpp.

Definition at line 59 of file spsc_ring_queue.inl.

◆ empty()

template<typename T >
bool CppCommon::SPSCRingQueue< T >::empty ( ) const
inlinenoexcept

Is ring queue empty?

Definition at line 53 of file spsc_ring_queue.h.

◆ Enqueue() [1/2]

template<typename T >
bool CppCommon::SPSCRingQueue< T >::Enqueue ( const T &  item)
inline

Enqueue an item into the ring queue (single producer thread method)

The item will be copied into the ring queue.

Will not block.

Parameters
item- Item to enqueue
Returns
'true' if the item was successfully enqueue, 'false' if the ring queue is full
Examples
threads_spsc_ring_queue.cpp.

Definition at line 33 of file spsc_ring_queue.inl.

◆ Enqueue() [2/2]

template<typename T >
bool CppCommon::SPSCRingQueue< T >::Enqueue ( T &&  item)
inline

Enqueue an item into the ring queue (single producer thread method)

The item will be moved into the ring queue.

Will not block.

Parameters
item- Item to enqueue
Returns
'true' if the item was successfully enqueue, 'false' if the ring queue is full

Definition at line 40 of file spsc_ring_queue.inl.

◆ operator bool()

template<typename T >
CppCommon::SPSCRingQueue< T >::operator bool ( ) const
inlineexplicitnoexcept

Check if the queue is not empty.

Definition at line 50 of file spsc_ring_queue.h.

◆ operator=() [1/2]

template<typename T >
SPSCRingQueue& CppCommon::SPSCRingQueue< T >::operator= ( const SPSCRingQueue< T > &  )
delete

◆ operator=() [2/2]

template<typename T >
SPSCRingQueue& CppCommon::SPSCRingQueue< T >::operator= ( SPSCRingQueue< T > &&  )
delete

◆ size()

template<typename T >
size_t CppCommon::SPSCRingQueue< T >::size
inlinenoexcept

Get ring queue size.

Definition at line 24 of file spsc_ring_queue.inl.


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