Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow::ConcurrentQueue< T, Container > Class Template Reference

#include <flow_concurrent_queue.h>

Public Member Functions

 ConcurrentQueue ()=default
 Constructs a concurrent FIFO queue.
 ConcurrentQueue (const allocator_type &allocator)
 Constructs a concurrent FIFO queue.
 ConcurrentQueue (const ConcurrentQueue &)=delete
 ConcurrentQueue (ConcurrentQueue &&)=delete
ConcurrentQueueoperator= (const ConcurrentQueue &)=delete
ConcurrentQueueoperator= (ConcurrentQueue &&)=delete
bool empty () const
 Checks if the queue is empty. Value can be obsolete in concurrency code.
std::size_t size () const
 Returns the number of elements in the queue. Value can be obsolete in concurrency code.
void push (const T &value)
 Pushes a new element into the queue.
void push (T &&value)
 Pushes a new element into the queue.
template<typename... Args>
void emplace (Args &&... args)
 Constructs a new element in place at the end of the queue.
std::optional< T > tryFront () const
 Tries to get a copy of the first element without removing it.
std::optional< T > tryPop ()
 Tries to pop and return the first element.
waitPop ()
 Waits until the queue is not empty, then pops and returns the first element.

Private Types

using allocator_type = typename Container::allocator_type

Private Attributes

std::mutex mux_
std::condition_variable blocked_
std::queue< T, Container > queue_

Detailed Description

template<typename T, typename Container = std::deque<T>>
class flow::ConcurrentQueue< T, Container >

Definition at line 10 of file flow_concurrent_queue.h.

Member Typedef Documentation

◆ allocator_type

template<typename T, typename Container = std::deque<T>>
using flow::ConcurrentQueue< T, Container >::allocator_type = typename Container::allocator_type
private

Definition at line 11 of file flow_concurrent_queue.h.

Constructor & Destructor Documentation

◆ ConcurrentQueue() [1/4]

template<typename T, typename Container = std::deque<T>>
flow::ConcurrentQueue< T, Container >::ConcurrentQueue ( )
default

Constructs a concurrent FIFO queue.

Referenced by ConcurrentQueue(), ConcurrentQueue(), operator=(), and operator=().

◆ ConcurrentQueue() [2/4]

template<typename T, typename Container = std::deque<T>>
flow::ConcurrentQueue< T, Container >::ConcurrentQueue ( const allocator_type & allocator)
inlineexplicit

Constructs a concurrent FIFO queue.

Definition at line 22 of file flow_concurrent_queue.h.

References queue_.

◆ ConcurrentQueue() [3/4]

template<typename T, typename Container = std::deque<T>>
flow::ConcurrentQueue< T, Container >::ConcurrentQueue ( const ConcurrentQueue< T, Container > & )
delete

References ConcurrentQueue().

◆ ConcurrentQueue() [4/4]

template<typename T, typename Container = std::deque<T>>
flow::ConcurrentQueue< T, Container >::ConcurrentQueue ( ConcurrentQueue< T, Container > && )
delete

References ConcurrentQueue().

Member Function Documentation

◆ emplace()

template<typename T, typename Container = std::deque<T>>
template<typename... Args>
void flow::ConcurrentQueue< T, Container >::emplace ( Args &&... args)
inline

Constructs a new element in place at the end of the queue.

Definition at line 64 of file flow_concurrent_queue.h.

64 {
65 {
67 queue_.emplace(std::forward<Args>(args)...);
68 }
69 blocked_.notify_one();
70 }
std::condition_variable blocked_

References blocked_, mux_, and queue_.

◆ empty()

template<typename T, typename Container = std::deque<T>>
bool flow::ConcurrentQueue< T, Container >::empty ( ) const
inline

Checks if the queue is empty. Value can be obsolete in concurrency code.

Returns
True if empty, false otherwise.

Definition at line 33 of file flow_concurrent_queue.h.

33 {
35 return queue_.empty();
36 }

References mux_, and queue_.

◆ operator=() [1/2]

template<typename T, typename Container = std::deque<T>>
ConcurrentQueue & flow::ConcurrentQueue< T, Container >::operator= ( ConcurrentQueue< T, Container > && )
delete

References ConcurrentQueue().

◆ operator=() [2/2]

template<typename T, typename Container = std::deque<T>>
ConcurrentQueue & flow::ConcurrentQueue< T, Container >::operator= ( const ConcurrentQueue< T, Container > & )
delete

References ConcurrentQueue().

◆ push() [1/2]

template<typename T, typename Container = std::deque<T>>
void flow::ConcurrentQueue< T, Container >::push ( const T & value)
inline

Pushes a new element into the queue.

Definition at line 45 of file flow_concurrent_queue.h.

45 {
46 {
48 queue_.push(value);
49 }
50 blocked_.notify_one();
51 }

References blocked_, mux_, and queue_.

◆ push() [2/2]

template<typename T, typename Container = std::deque<T>>
void flow::ConcurrentQueue< T, Container >::push ( T && value)
inline

Pushes a new element into the queue.

Definition at line 54 of file flow_concurrent_queue.h.

54 {
55 {
57 queue_.push(std::move(value));
58 }
59 blocked_.notify_one();
60 }

References blocked_, mux_, and queue_.

◆ size()

template<typename T, typename Container = std::deque<T>>
std::size_t flow::ConcurrentQueue< T, Container >::size ( ) const
inline

Returns the number of elements in the queue. Value can be obsolete in concurrency code.

Definition at line 39 of file flow_concurrent_queue.h.

39 {
41 return queue_.size();
42 }

References mux_, and queue_.

◆ tryFront()

template<typename T, typename Container = std::deque<T>>
std::optional< T > flow::ConcurrentQueue< T, Container >::tryFront ( ) const
inline

Tries to get a copy of the first element without removing it.

Returns
A copy of the first element or std::nullopt if empty.

Definition at line 74 of file flow_concurrent_queue.h.

74 {
76 if (queue_.empty()) {
77 return std::nullopt;
78 }
79 return queue_.front();
80 }

References mux_, and queue_.

◆ tryPop()

template<typename T, typename Container = std::deque<T>>
std::optional< T > flow::ConcurrentQueue< T, Container >::tryPop ( )
inline

Tries to pop and return the first element.

Returns
The first element moved or std::nullopt if empty.

Definition at line 84 of file flow_concurrent_queue.h.

84 {
86 if (queue_.empty()) {
87 return std::nullopt;
88 }
90
91 queue_.pop();
92 return value;
93 }

References mux_, and queue_.

◆ waitPop()

template<typename T, typename Container = std::deque<T>>
T flow::ConcurrentQueue< T, Container >::waitPop ( )
inline

Waits until the queue is not empty, then pops and returns the first element.

Returns
The first element.

Definition at line 97 of file flow_concurrent_queue.h.

97 {
99 blocked_.wait(lock, [&]() { return !queue_.empty(); });
100
101 T value = std::move(queue_.front());
102 queue_.pop();
103 return value;
104 }

References blocked_, mux_, and queue_.

Member Data Documentation

◆ blocked_

template<typename T, typename Container = std::deque<T>>
std::condition_variable flow::ConcurrentQueue< T, Container >::blocked_
private

Definition at line 14 of file flow_concurrent_queue.h.

Referenced by emplace(), push(), push(), and waitPop().

◆ mux_

template<typename T, typename Container = std::deque<T>>
std::mutex flow::ConcurrentQueue< T, Container >::mux_
mutableprivate

Definition at line 13 of file flow_concurrent_queue.h.

Referenced by emplace(), empty(), push(), push(), size(), tryFront(), tryPop(), and waitPop().

◆ queue_

template<typename T, typename Container = std::deque<T>>
std::queue<T, Container> flow::ConcurrentQueue< T, Container >::queue_
private

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