Skip to main content

BlockingQueue Class Template

Thread-safe bounded blocking queue used by the runtime mailboxes. More...

Declaration

template <class T> class simaai::neat::graph::runtime::BlockingQueue<T> { ... }

Included Headers

#include <BlockingQueue.h>

Public Constructors Index

template <class T>
BlockingQueue (std::size_t capacity=0)

Construct a queue with the given capacity (0 = unbounded). More...

Public Member Functions Index

template <class T>
boolpush (const T &item, int timeout_ms=-1)

Push item (copy). More...

template <class T>
boolpush (T &&item, int timeout_ms=-1)

Push item (move). More...

template <class T>
booltry_push (const T &item)

Non-blocking copy push; returns false if closed or full. More...

template <class T>
booltry_push (T &&item)

Non-blocking move push; returns false if closed or full. More...

template <class T>
boolpop (T &out, int timeout_ms=-1)

Pop the next item into out. More...

template <class T>
voidclose ()

Close the queue: wakes blocked threads and refuses further pushes. More...

template <class T>
boolclosed () const

Returns true iff the queue has been closed. More...

template <class T>
std::size_tsize () const

Current queue size. More...

Private Member Attributes Index

template <class T>
std::mutexmu_
template <class T>
std::condition_variablecv_not_empty_
template <class T>
std::condition_variablecv_not_full_
template <class T>
std::deque< T >queue_
template <class T>
std::size_tcapacity_ = 0
template <class T>
boolclosed_ = false

Description

Thread-safe bounded blocking queue used by the runtime mailboxes.

Supports blocking and non-blocking enqueue/dequeue with optional millisecond timeouts, and a close() operation that wakes blocked waiters and refuses further pushes. A capacity of 0 means unbounded.

Template Parameters
T

Element type stored in the queue.

See Also

StageMailbox

Definition at line 29 of file BlockingQueue.h.

Public Constructors

BlockingQueue()

template <class T>
simaai::neat::graph::runtime::BlockingQueue< T >::BlockingQueue (std::size_t capacity=0)
inline explicit

Construct a queue with the given capacity (0 = unbounded).

Definition at line 32 of file BlockingQueue.h.

32 explicit BlockingQueue(std::size_t capacity = 0) : capacity_(capacity) {}

Public Member Functions

close()

template <class T>
void simaai::neat::graph::runtime::BlockingQueue< T >::close ()
inline

Close the queue: wakes blocked threads and refuses further pushes.

Definition at line 119 of file BlockingQueue.h.

119 void close() {
120 std::lock_guard<std::mutex> lock(mu_);
121 closed_ = true;
122 cv_not_empty_.notify_all();
123 cv_not_full_.notify_all();
124 }

closed()

template <class T>
bool simaai::neat::graph::runtime::BlockingQueue< T >::closed ()
inline

Returns true iff the queue has been closed.

Definition at line 127 of file BlockingQueue.h.

127 bool closed() const {
128 std::lock_guard<std::mutex> lock(mu_);
129 return closed_;
130 }

pop()

template <class T>
bool simaai::neat::graph::runtime::BlockingQueue< T >::pop (T & out, int timeout_ms=-1)
inline

Pop the next item into out.

Blocks up to timeout_ms (or forever if -1). Returns false if closed and empty (or on timeout).

Definition at line 102 of file BlockingQueue.h.

102 bool pop(T& out, int timeout_ms = -1) {
103 std::unique_lock<std::mutex> lock(mu_);
104 if (timeout_ms < 0) {
105 cv_not_empty_.wait(lock, [&] { return closed_ || !queue_.empty(); });
106 } else {
107 cv_not_empty_.wait_for(lock, std::chrono::milliseconds(timeout_ms),
108 [&] { return closed_ || !queue_.empty(); });
109 }
110 if (queue_.empty())
111 return false;
112 out = std::move(queue_.front());
113 queue_.pop_front();
114 cv_not_full_.notify_one();
115 return true;
116 }

push()

template <class T>
bool simaai::neat::graph::runtime::BlockingQueue< T >::push (const T & item, int timeout_ms=-1)
inline

Push item (copy).

Blocks up to timeout_ms (or forever if -1). Returns false on close/timeout.

Definition at line 36 of file BlockingQueue.h.

36 bool push(const T& item, int timeout_ms = -1) {
37 std::unique_lock<std::mutex> lock(mu_);
38 if (closed_)
39 return false;
40 if (capacity_ > 0) {
41 if (timeout_ms < 0) {
42 cv_not_full_.wait(lock, [&] { return closed_ || queue_.size() < capacity_; });
43 } else if (!cv_not_full_.wait_for(lock, std::chrono::milliseconds(timeout_ms),
44 [&] { return closed_ || queue_.size() < capacity_; })) {
45 return false;
46 }
47 if (closed_ || (capacity_ > 0 && queue_.size() >= capacity_))
48 return false;
49 }
50 queue_.push_back(item);
51 cv_not_empty_.notify_one();
52 return true;
53 }

push()

template <class T>
bool simaai::neat::graph::runtime::BlockingQueue< T >::push (T && item, int timeout_ms=-1)
inline

Push item (move).

Blocks up to timeout_ms (or forever if -1). Returns false on close/timeout.

Definition at line 57 of file BlockingQueue.h.

57 bool push(T&& item, int timeout_ms = -1) {
58 std::unique_lock<std::mutex> lock(mu_);
59 if (closed_)
60 return false;
61 if (capacity_ > 0) {
62 if (timeout_ms < 0) {
63 cv_not_full_.wait(lock, [&] { return closed_ || queue_.size() < capacity_; });
64 } else if (!cv_not_full_.wait_for(lock, std::chrono::milliseconds(timeout_ms),
65 [&] { return closed_ || queue_.size() < capacity_; })) {
66 return false;
67 }
68 if (closed_ || (capacity_ > 0 && queue_.size() >= capacity_))
69 return false;
70 }
71 queue_.push_back(std::move(item));
72 cv_not_empty_.notify_one();
73 return true;
74 }

size()

template <class T>
std::size_t simaai::neat::graph::runtime::BlockingQueue< T >::size ()
inline

Current queue size.

Definition at line 133 of file BlockingQueue.h.

133 std::size_t size() const {
134 std::lock_guard<std::mutex> lock(mu_);
135 return queue_.size();
136 }

try_push()

template <class T>
bool simaai::neat::graph::runtime::BlockingQueue< T >::try_push (const T & item)
inline

Non-blocking copy push; returns false if closed or full.

Definition at line 77 of file BlockingQueue.h.

77 bool try_push(const T& item) {
78 std::lock_guard<std::mutex> lock(mu_);
79 if (closed_)
80 return false;
81 if (capacity_ > 0 && queue_.size() >= capacity_)
82 return false;
83 queue_.push_back(item);
84 cv_not_empty_.notify_one();
85 return true;
86 }

try_push()

template <class T>
bool simaai::neat::graph::runtime::BlockingQueue< T >::try_push (T && item)
inline

Non-blocking move push; returns false if closed or full.

Definition at line 89 of file BlockingQueue.h.

89 bool try_push(T&& item) {
90 std::lock_guard<std::mutex> lock(mu_);
91 if (closed_)
92 return false;
93 if (capacity_ > 0 && queue_.size() >= capacity_)
94 return false;
95 queue_.push_back(std::move(item));
96 cv_not_empty_.notify_one();
97 return true;
98 }

Private Member Attributes

capacity_

template <class T>
std::size_t simaai::neat::graph::runtime::BlockingQueue< T >::capacity_ = 0

Definition at line 143 of file BlockingQueue.h.

143 std::size_t capacity_ = 0;

closed_

template <class T>
bool simaai::neat::graph::runtime::BlockingQueue< T >::closed_ = false

Definition at line 144 of file BlockingQueue.h.

144 bool closed_ = false;

cv_not_empty_

template <class T>
std::condition_variable simaai::neat::graph::runtime::BlockingQueue< T >::cv_not_empty_

Definition at line 140 of file BlockingQueue.h.

140 std::condition_variable cv_not_empty_;

cv_not_full_

template <class T>
std::condition_variable simaai::neat::graph::runtime::BlockingQueue< T >::cv_not_full_

Definition at line 141 of file BlockingQueue.h.

141 std::condition_variable cv_not_full_;

mu_

template <class T>
std::mutex simaai::neat::graph::runtime::BlockingQueue< T >::mu_
mutable

Definition at line 139 of file BlockingQueue.h.

139 mutable std::mutex mu_;

queue_

template <class T>
std::deque<T> simaai::neat::graph::runtime::BlockingQueue< T >::queue_

Definition at line 142 of file BlockingQueue.h.

142 std::deque<T> queue_;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.