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

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

#include <wait_queue.h>

Public Member Functions

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

Detailed Description

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

Multiple producers / multiple consumers wait queue.

Multiple producers / multiple consumers wait queue provides a classic solution for producer-consumer problem using queue 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_queue.cpp.

Definition at line 29 of file wait_queue.h.

Constructor & Destructor Documentation

◆ WaitQueue() [1/3]

template<typename T >
CppCommon::WaitQueue< T >::WaitQueue ( size_t  capacity = 0)
inlineexplicit

Default class constructor.

Parameters
capacity- Wait queue capacity (default is 0 for unlimited capacity)

Definition at line 12 of file wait_queue.inl.

◆ WaitQueue() [2/3]

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

◆ WaitQueue() [3/3]

template<typename T >
CppCommon::WaitQueue< T >::WaitQueue ( WaitQueue< T > &&  )
default

◆ ~WaitQueue()

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

Definition at line 17 of file wait_queue.inl.

Member Function Documentation

◆ capacity()

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

Get wait queue capacity.

Definition at line 30 of file wait_queue.inl.

◆ Close()

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

Close the wait queue.

Will block.

Examples
threads_wait_queue.cpp.

Definition at line 120 of file wait_queue.inl.

◆ closed()

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

Is wait queue closed?

Definition at line 23 of file wait_queue.inl.

◆ Dequeue()

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

Dequeue an item from the wait queue.

The item will be moved from the wait queue.

Will block.

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

Definition at line 95 of file wait_queue.inl.

◆ empty()

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

Is wait queue empty?

Definition at line 51 of file wait_queue.h.

◆ Enqueue() [1/2]

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

Enqueue an item into the wait queue.

The item will be copied into the wait queue.

Will block.

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

Definition at line 47 of file wait_queue.inl.

◆ Enqueue() [2/2]

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

Enqueue an item into the wait queue.

The item will be moved into the wait queue.

Will block.

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

Definition at line 71 of file wait_queue.inl.

◆ operator bool()

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

Check if the wait queue is not empty.

Definition at line 45 of file wait_queue.h.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

template<typename T >
WaitQueue& CppCommon::WaitQueue< T >::operator= ( WaitQueue< T > &&  )
default

◆ size()

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

Get wait queue size.

Definition at line 40 of file wait_queue.inl.


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