PIP 5.6.1
Platform-Independent Primitives
Public Member Functions | List of all members
PIBlockingQueue< T > Class Template Reference

Thread-safe queue that supports blocking operations - waits for space when storing and waits for element when retrieving. More...

#include <piblockingqueue.h>

Inheritance diagram for PIBlockingQueue< T >:
Inheritance graph
[legend]

Public Member Functions

 PIBlockingQueue (size_t capacity=SIZE_MAX, PIConditionVariable *cond_var_add=new PIConditionVariable(), PIConditionVariable *cond_var_rem=new PIConditionVariable())
 Constructs queue with capacity capacity. More...
 
 PIBlockingQueue (const PIDeque< T > &other)
 Constructs queue from snapshot of other without synchronizing access to the source deque.
 
 PIBlockingQueue (PIBlockingQueue< T > &other)
 Constructs queue by copying another blocking queue while locking both queues.
 
 ~PIBlockingQueue ()
 Destroys queue and owned condition variables.
 
PIBlockingQueue< T > & put (const T &v)
 Appends v, waiting indefinitely until space becomes available when the queue is full.
 
PIBlockingQueue< T > & enqueue (const T &v)
 Alias for put().
 
bool offer (const T &v, PISystemTime timeout={})
 Tries to append v. More...
 
take ()
 Removes and returns the front element, waiting indefinitely until one becomes available.
 
dequeue ()
 Alias for take().
 
poll (PISystemTime timeout={}, const T &defaultVal=T(), bool *isOk=nullptr)
 Tries to remove and return the front element. More...
 
size_t capacity ()
 Returns configured capacity limit. More...
 
size_t remainingCapacity ()
 Returns how many more elements can be inserted without blocking at the moment of the call.
 
size_t size ()
 Returns current number of queued elements.
 
size_t drainTo (PIDeque< T > &other, size_t maxCount=SIZE_MAX)
 Moves up to maxCount currently available elements into deque other without waiting.
 
size_t drainTo (PIBlockingQueue< T > &other, size_t maxCount=SIZE_MAX)
 Moves up to maxCount currently available elements into blocking queue other without waiting. More...
 

Detailed Description

template<typename T>
class PIBlockingQueue< T >

Thread-safe queue that supports blocking operations - waits for space when storing and waits for element when retrieving.

Constructor & Destructor Documentation

◆ PIBlockingQueue()

template<typename T >
PIBlockingQueue< T >::PIBlockingQueue ( size_t  capacity = SIZE_MAX,
PIConditionVariable cond_var_add = new PIConditionVariable(),
PIConditionVariable cond_var_rem = new PIConditionVariable() 
)
inlineexplicit

Constructs queue with capacity capacity.

Passed condition variables become owned by the queue and are deleted with it.

Member Function Documentation

◆ offer()

template<typename T >
bool PIBlockingQueue< T >::offer ( const T &  v,
PISystemTime  timeout = {} 
)
inline

Tries to append v.

With null timeout this method checks capacity once and returns immediately.

◆ poll()

template<typename T >
T PIBlockingQueue< T >::poll ( PISystemTime  timeout = {},
const T &  defaultVal = T(),
bool *  isOk = nullptr 
)
inline

Tries to remove and return the front element.

With null timeout this method checks once and returns defaultVal immediately if the queue is empty. If isOk is not null, it is set to true on successful retrieval.

◆ capacity()

template<typename T >
size_t PIBlockingQueue< T >::capacity ( )
inline

Returns configured capacity limit.

For the default unbounded queue this value is SIZE_MAX.

◆ drainTo()

template<typename T >
size_t PIBlockingQueue< T >::drainTo ( PIBlockingQueue< T > &  other,
size_t  maxCount = SIZE_MAX 
)
inline

Moves up to maxCount currently available elements into blocking queue other without waiting.

The actual count is also limited by the remaining capacity of other.