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

Multiple producers / multiple consumers wait-free ring queue. More...

#include <mpmc_ring_queue.h>

Public Member Functions

 MPMCRingQueue (size_t capacity)
 Default class constructor. More...
 
 MPMCRingQueue (const MPMCRingQueue &)=delete
 
 MPMCRingQueue (MPMCRingQueue &&)=delete
 
 ~MPMCRingQueue ()
 
MPMCRingQueueoperator= (const MPMCRingQueue &)=delete
 
MPMCRingQueueoperator= (MPMCRingQueue &&)=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 (multiple producers threads method) More...
 
bool Enqueue (T &&item)
 Enqueue an item into the ring queue (multiple producers threads method) More...
 
bool Dequeue (T &item)
 Dequeue an item from the ring queue (multiple consumers threads method) More...
 

Detailed Description

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

Multiple producers / multiple consumers wait-free ring queue.

Multiple producers / multiple consumers wait-free ring queue use only atomic operations to provide thread-safe enqueue and dequeue operations. Ring queue size is limited to the capacity provided in the constructor.

FIFO order is guaranteed!

Thread-safe.

C++ implementation of Dmitry Vyukov's non-intrusive lock free unbound MPSC queue http://www.1024cores.net/home/lock-free-algorithms/queues/non-intrusive-mpsc-node-based-queue

Examples
threads_mpmc_ring_queue.cpp.

Definition at line 33 of file mpmc_ring_queue.h.

Constructor & Destructor Documentation

◆ MPMCRingQueue() [1/3]

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

Default class constructor.

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

Definition at line 12 of file mpmc_ring_queue.inl.

◆ MPMCRingQueue() [2/3]

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

◆ MPMCRingQueue() [3/3]

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

◆ ~MPMCRingQueue()

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

Definition at line 43 of file mpmc_ring_queue.h.

Member Function Documentation

◆ capacity()

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

Get ring queue capacity.

Definition at line 54 of file mpmc_ring_queue.h.

◆ Dequeue()

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

Dequeue an item from the ring queue (multiple consumers threads 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_mpmc_ring_queue.cpp.

Definition at line 89 of file mpmc_ring_queue.inl.

◆ empty()

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

Is ring queue empty?

Definition at line 52 of file mpmc_ring_queue.h.

◆ Enqueue() [1/2]

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

Enqueue an item into the ring queue (multiple producers threads 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_mpmc_ring_queue.cpp.

Definition at line 37 of file mpmc_ring_queue.inl.

◆ Enqueue() [2/2]

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

Enqueue an item into the ring queue (multiple producers threads 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 44 of file mpmc_ring_queue.inl.

◆ operator bool()

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

Check if the queue is not empty.

Definition at line 49 of file mpmc_ring_queue.h.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ size()

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

Get ring queue size.

Definition at line 28 of file mpmc_ring_queue.inl.


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