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

Multiple producers / multiple consumers wait ring. More...

#include <wait_ring.h>

Public Member Functions

 WaitRing (size_t capacity)
 Default class constructor. More...
 
 WaitRing (const WaitRing &)=delete
 
 WaitRing (WaitRing &&)=delete
 
 ~WaitRing ()
 
WaitRingoperator= (const WaitRing &)=delete
 
WaitRingoperator= (WaitRing &&)=delete
 
 operator bool () const noexcept
 Check if the wait ring is not empty. More...
 
bool closed () const
 Is wait ring closed? More...
 
bool empty () const
 Is wait ring empty? More...
 
size_t capacity () const
 Get wait ring capacity. More...
 
size_t size () const
 Get wait ring size. More...
 
bool Enqueue (const T &item)
 Enqueue an item into the wait ring. More...
 
bool Enqueue (T &&item)
 Enqueue an item into the wait ring. More...
 
bool Dequeue (T &item)
 Dequeue an item from the wait ring. More...
 
void Close ()
 Close the wait ring. More...
 

Detailed Description

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

Multiple producers / multiple consumers wait ring.

Multiple producers / multiple consumers wait ring provides a classic solution for producer-consumer problem using fixed ring and monitor synchronization primitive (mutex with condition variable).

FIFO order is guaranteed!

https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem

Examples
threads_wait_ring.cpp.

Definition at line 30 of file wait_ring.h.

Constructor & Destructor Documentation

◆ WaitRing() [1/3]

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

Default class constructor.

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

Definition at line 12 of file wait_ring.inl.

◆ WaitRing() [2/3]

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

◆ WaitRing() [3/3]

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

◆ ~WaitRing()

template<typename T >
CppCommon::WaitRing< T >::~WaitRing
inline

Definition at line 19 of file wait_ring.inl.

Member Function Documentation

◆ capacity()

template<typename T >
size_t CppCommon::WaitRing< T >::capacity ( ) const
inline

Get wait ring capacity.

Definition at line 54 of file wait_ring.h.

◆ Close()

template<typename T >
void CppCommon::WaitRing< T >::Close
inline

Close the wait ring.

Will block.

Examples
threads_wait_ring.cpp.

Definition at line 94 of file wait_ring.inl.

◆ closed()

template<typename T >
bool CppCommon::WaitRing< T >::closed
inline

Is wait ring closed?

Definition at line 25 of file wait_ring.inl.

◆ Dequeue()

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

Dequeue an item from the wait ring.

The item will be moved from the wait ring.

Will block.

Parameters
item- Item to dequeue
Returns
'true' if the item was successfully dequeue, 'false' if the wait ring is closed
Examples
threads_wait_ring.cpp.

Definition at line 70 of file wait_ring.inl.

◆ empty()

template<typename T >
bool CppCommon::WaitRing< T >::empty ( ) const
inline

Is wait ring empty?

Definition at line 52 of file wait_ring.h.

◆ Enqueue() [1/2]

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

Enqueue an item into the wait ring.

The item will be copied into the wait ring.

Will block.

Parameters
item- Item to enqueue
Returns
'true' if the item was successfully enqueue, 'false' if the wait ring is closed
Examples
threads_wait_ring.cpp.

Definition at line 39 of file wait_ring.inl.

◆ Enqueue() [2/2]

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

Enqueue an item into the wait ring.

The item will be moved into the wait ring.

Will block.

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

Definition at line 46 of file wait_ring.inl.

◆ operator bool()

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

Check if the wait ring is not empty.

Definition at line 46 of file wait_ring.h.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ size()

template<typename T >
size_t CppCommon::WaitRing< T >::size
inline

Get wait ring size.

Definition at line 32 of file wait_ring.inl.


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