Skip to main content

BlockingQueue Class Template

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)

Public Member Functions Index

template <class T>
boolpush (const T &item, int timeout_ms=-1)
template <class T>
boolpush (T &&item, int timeout_ms=-1)
template <class T>
booltry_push (const T &item)
template <class T>
booltry_push (T &&item)
template <class T>
boolpop (T &out, int timeout_ms=-1)
template <class T>
voidclose ()
template <class T>
boolclosed () const
template <class T>
std::size_tsize () const

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

Definition at line 17 of file BlockingQueue.h.

Public Constructors

BlockingQueue()

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

Definition at line 19 of file BlockingQueue.h.

19 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

Definition at line 97 of file BlockingQueue.h.

97 void close() {
98 std::lock_guard<std::mutex> lock(mu_);
99 closed_ = true;
100 cv_not_empty_.notify_all();
101 cv_not_full_.notify_all();
102 }

closed()

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

Definition at line 104 of file BlockingQueue.h.

104 bool closed() const {
105 std::lock_guard<std::mutex> lock(mu_);
106 return closed_;
107 }

pop()

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

Definition at line 81 of file BlockingQueue.h.

81 bool pop(T& out, int timeout_ms = -1) {
82 std::unique_lock<std::mutex> lock(mu_);
83 if (timeout_ms < 0) {
84 cv_not_empty_.wait(lock, [&] { return closed_ || !queue_.empty(); });
85 } else {
86 cv_not_empty_.wait_for(lock, std::chrono::milliseconds(timeout_ms),
87 [&] { return closed_ || !queue_.empty(); });
88 }
89 if (queue_.empty())
90 return false;
91 out = std::move(queue_.front());
92 queue_.pop_front();
93 cv_not_full_.notify_one();
94 return true;
95 }

push()

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

Definition at line 21 of file BlockingQueue.h.

21 bool push(const T& item, int timeout_ms = -1) {
22 std::unique_lock<std::mutex> lock(mu_);
23 if (closed_)
24 return false;
25 if (capacity_ > 0) {
26 if (timeout_ms < 0) {
27 cv_not_full_.wait(lock, [&] { return closed_ || queue_.size() < capacity_; });
28 } else if (!cv_not_full_.wait_for(lock, std::chrono::milliseconds(timeout_ms),
29 [&] { return closed_ || queue_.size() < capacity_; })) {
30 return false;
31 }
32 if (closed_ || (capacity_ > 0 && queue_.size() >= capacity_))
33 return false;
34 }
35 queue_.push_back(item);
36 cv_not_empty_.notify_one();
37 return true;
38 }

push()

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

Definition at line 40 of file BlockingQueue.h.

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

size()

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

Definition at line 109 of file BlockingQueue.h.

109 std::size_t size() const {
110 std::lock_guard<std::mutex> lock(mu_);
111 return queue_.size();
112 }

try_push()

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

Definition at line 59 of file BlockingQueue.h.

59 bool try_push(const T& item) {
60 std::lock_guard<std::mutex> lock(mu_);
61 if (closed_)
62 return false;
63 if (capacity_ > 0 && queue_.size() >= capacity_)
64 return false;
65 queue_.push_back(item);
66 cv_not_empty_.notify_one();
67 return true;
68 }

try_push()

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

Definition at line 70 of file BlockingQueue.h.

70 bool try_push(T&& item) {
71 std::lock_guard<std::mutex> lock(mu_);
72 if (closed_)
73 return false;
74 if (capacity_ > 0 && queue_.size() >= capacity_)
75 return false;
76 queue_.push_back(std::move(item));
77 cv_not_empty_.notify_one();
78 return true;
79 }

Private Member Attributes

capacity_

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

Definition at line 119 of file BlockingQueue.h.

119 std::size_t capacity_ = 0;

closed_

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

Definition at line 120 of file BlockingQueue.h.

120 bool closed_ = false;

cv_not_empty_

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

Definition at line 116 of file BlockingQueue.h.

116 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 117 of file BlockingQueue.h.

117 std::condition_variable cv_not_full_;

mu_

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

Definition at line 115 of file BlockingQueue.h.

115 mutable std::mutex mu_;

queue_

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

Definition at line 118 of file BlockingQueue.h.

118 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.1.