6#include <condition_variable>
19 std::lock_guard lock(mutex);
26 std::lock_guard lock(mutex);
32 std::unique_lock<std::mutex> lock(mutex);
34 T
front = std::move(queue.front());
46 inline void push(T&& value) {
48 std::lock_guard<std::mutex> lock(mutex);
56 inline void push(
const T& value) {
58 std::lock_guard<std::mutex> lock(mutex);
68 std::lock_guard<std::mutex> lock(mutex);
84 template <
class Predicate>
86 std::chrono::milliseconds timeout = std::chrono::milliseconds(0)) {
87 std::unique_lock<std::mutex> lock(mutex);
89 if (timeout.count() > 0) {
90 cv.wait_for(lock, timeout, [&]() {
return (
false == queue.empty()) or pred(); });
92 cv.wait(lock, [&]() {
return (
false == queue.empty()) or pred(); });
98 template <
class Predicate>
99 inline void wait_on_custom_event(Predicate pred, std::chrono::milliseconds timeout = std::chrono::milliseconds(0)) {
100 std::unique_lock<std::mutex> lock(mutex);
102 if (timeout.count() > 0) {
103 cv.wait_for(lock, timeout, [&]() {
return pred(); });
105 cv.wait(lock, [&]() {
return pred(); });
117 mutable std::mutex mutex;
118 std::condition_variable cv;
Thread safe message queue. Holds a conditional variable that can be waited upon. Will take up the wai...
Definition: safe_queue.hpp:15
void wait_on_custom_event(Predicate pred, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
Waits on the queue for a custom event.
Definition: safe_queue.hpp:99
void notify_waiting_thread()
Notifies a single waiting thread to wake up.
Definition: safe_queue.hpp:110
void clear()
Clears the queue.
Definition: safe_queue.hpp:66
void push(T &&value)
Queues an element and notifies any threads waiting on the internal conditional variable.
Definition: safe_queue.hpp:46
T front()
We return a copy here, since while might be accessing the reference while another thread uses pop and...
Definition: safe_queue.hpp:25
T pop()
Definition: safe_queue.hpp:31
void wait_on_queue_element_or_predicate(Predicate pred, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
Same as 'wait_on_queue' but receives an additional predicate to wait upon.
Definition: safe_queue.hpp:85
bool empty() const
Definition: safe_queue.hpp:18
void wait_on_queue_element(std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
Waits for the queue to receive an element.
Definition: safe_queue.hpp:79
void push(const T &value)
Queues an element and notifies any threads waiting on the internal conditional variable.
Definition: safe_queue.hpp:56