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

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

#include <mpsc_ring_queue.h>

Public Member Functions

 MPSCRingQueue (size_t capacity, size_t concurrency=std::thread::hardware_concurrency())
 Default class constructor. More...
 
 MPSCRingQueue (const MPSCRingQueue &)=delete
 
 MPSCRingQueue (MPSCRingQueue &&)=delete
 
 ~MPSCRingQueue ()=default
 
MPSCRingQueueoperator= (const MPSCRingQueue &)=delete
 
MPSCRingQueueoperator= (MPSCRingQueue &&)=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 concurrency () const noexcept
 Get ring queue concurrency. 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 (single consumer threads method) More...
 
bool Dequeue (const std::function< void(const T &)> &handler=[](const int &){})
 Dequeue all items from the linked queue (single consumer thread method) More...
 

Detailed Description

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

Multiple producers / single consumer wait-free ring queue.

Multiple producers / single consumer wait-free ring queue use only atomic operations to provide thread-safe enqueue and dequeue operations. This data structure consist of several SPSC ring queues 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 queue sizes are limited to the capacity provided in the constructor.

FIFO order is not guaranteed!

Thread-safe.

Examples
threads_mpsc_ring_queue.cpp.

Definition at line 39 of file mpsc_ring_queue.h.

Constructor & Destructor Documentation

◆ MPSCRingQueue() [1/3]

template<typename T >
CppCommon::MPSCRingQueue< T >::MPSCRingQueue ( size_t  capacity,
size_t  concurrency = std::thread::hardware_concurrency() 
)
inlineexplicit

Default class constructor.

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

Definition at line 12 of file mpsc_ring_queue.inl.

◆ MPSCRingQueue() [2/3]

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

◆ MPSCRingQueue() [3/3]

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

◆ ~MPSCRingQueue()

template<typename T >
CppCommon::MPSCRingQueue< T >::~MPSCRingQueue ( )
default

Member Function Documentation

◆ capacity()

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

Get ring queue capacity.

Definition at line 61 of file mpsc_ring_queue.h.

◆ concurrency()

template<typename T >
size_t CppCommon::MPSCRingQueue< T >::concurrency ( ) const
inlinenoexcept

Get ring queue concurrency.

Definition at line 63 of file mpsc_ring_queue.h.

◆ Dequeue() [1/2]

template<typename T >
bool CppCommon::MPSCRingQueue< T >::Dequeue ( const std::function< void(const T &)> &  handler = [](const int&){})
inline

Dequeue all items from the linked queue (single consumer thread method)

All items in the batcher will be processed by the given handler.

Will not block.

Parameters
handler- Batch handler (default is empty handler)
Returns
'true' if all items were successfully handled, 'false' if the linked batcher is empty

Definition at line 62 of file mpsc_ring_queue.inl.

◆ Dequeue() [2/2]

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

Dequeue an item from the ring queue (single consumer 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_mpsc_ring_queue.cpp.

Definition at line 49 of file mpsc_ring_queue.inl.

◆ empty()

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

Is ring queue empty?

Definition at line 59 of file mpsc_ring_queue.h.

◆ Enqueue() [1/2]

template<typename T >
bool CppCommon::MPSCRingQueue< 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_mpsc_ring_queue.cpp.

Definition at line 29 of file mpsc_ring_queue.inl.

◆ Enqueue() [2/2]

template<typename T >
bool CppCommon::MPSCRingQueue< 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 36 of file mpsc_ring_queue.inl.

◆ operator bool()

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

Check if the queue is not empty.

Definition at line 56 of file mpsc_ring_queue.h.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ size()

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

Get ring queue size.

Definition at line 20 of file mpsc_ring_queue.inl.


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