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

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

#include <wait_batcher.h>

Public Member Functions

 WaitBatcher (size_t capacity=0, size_t initial=0)
 Default class constructor. More...
 
 WaitBatcher (const WaitBatcher &)=delete
 
 WaitBatcher (WaitBatcher &&)=delete
 
 ~WaitBatcher ()
 
WaitBatcheroperator= (const WaitBatcher &)=delete
 
WaitBatcheroperator= (WaitBatcher &&)=delete
 
 operator bool () const noexcept
 Check if the wait batcher is not empty. More...
 
bool closed () const
 Is wait batcher closed? More...
 
bool empty () const
 Is wait batcher empty? More...
 
size_t capacity () const
 Get wait batcher capacity. More...
 
size_t size () const
 Get wait batcher size. More...
 
bool Enqueue (const T &item)
 Enqueue an item into the wait batcher. More...
 
bool Enqueue (T &&item)
 Enqueue an item into the wait batcher. More...
 
template<class InputIterator >
bool Enqueue (InputIterator first, InputIterator last)
 Enqueue all items into the wait batcher. More...
 
bool Dequeue (std::vector< T > &items)
 Dequeue all items from the wait batcher. More...
 
void Close ()
 Close the wait batcher. More...
 

Detailed Description

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

Multiple producers / multiple consumers wait batcher.

Multiple producers / multiple consumers wait batcher provides a classic solution for producer-consumer problem using two vectors and hot-swap technique with monitor synchronization primitive (mutex with condition variable). It allows a consumer thread to process all items in queue in a batch mode.

FIFO order is guaranteed!

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

Examples
threads_wait_batcher.cpp.

Definition at line 31 of file wait_batcher.h.

Constructor & Destructor Documentation

◆ WaitBatcher() [1/3]

template<typename T >
CppCommon::WaitBatcher< T >::WaitBatcher ( size_t  capacity = 0,
size_t  initial = 0 
)
inlineexplicit

Default class constructor.

Parameters
capacity- Wait batcher capacity (0 for unlimited capacity, default is 0)
initial- Initial wait batcher capacity (default is 0)

Definition at line 12 of file wait_batcher.inl.

◆ WaitBatcher() [2/3]

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

◆ WaitBatcher() [3/3]

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

◆ ~WaitBatcher()

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

Definition at line 18 of file wait_batcher.inl.

Member Function Documentation

◆ capacity()

template<typename T >
size_t CppCommon::WaitBatcher< T >::capacity
inline

Get wait batcher capacity.

Definition at line 31 of file wait_batcher.inl.

◆ Close()

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

Close the wait batcher.

Will block.

Examples
threads_wait_batcher.cpp.

Definition at line 149 of file wait_batcher.inl.

◆ closed()

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

Is wait batcher closed?

Definition at line 24 of file wait_batcher.inl.

◆ Dequeue()

template<typename T >
bool CppCommon::WaitBatcher< T >::Dequeue ( std::vector< T > &  items)
inline

Dequeue all items from the wait batcher.

Will block.

Parameters
items- Items to dequeue
Returns
'true' if all items was successfully dequeue, 'false' if the wait batcher is closed
Examples
threads_wait_batcher.cpp.

Definition at line 121 of file wait_batcher.inl.

◆ empty()

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

Is wait batcher empty?

Definition at line 54 of file wait_batcher.h.

◆ Enqueue() [1/3]

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

Enqueue an item into the wait batcher.

The item will be copied into the wait batcher.

Will block.

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

Definition at line 48 of file wait_batcher.inl.

◆ Enqueue() [2/3]

template<typename T >
template<class InputIterator >
bool CppCommon::WaitBatcher< T >::Enqueue ( InputIterator  first,
InputIterator  last 
)
inline

Enqueue all items into the wait batcher.

All items will be copied into the wait batcher.

Will block.

Parameters
first- Iterator to the first item
last- Iterator to the last item
Returns
'true' if all items were successfully enqueue, 'false' if the wait batcher is closed

Definition at line 97 of file wait_batcher.inl.

◆ Enqueue() [3/3]

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

Enqueue an item into the wait batcher.

The item will be moved into the wait batcher.

Will block.

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

Definition at line 72 of file wait_batcher.inl.

◆ operator bool()

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

Check if the wait batcher is not empty.

Definition at line 48 of file wait_batcher.h.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ size()

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

Get wait batcher size.

Definition at line 41 of file wait_batcher.inl.


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