21 #ifndef OPENLIBRARY_PALGORITHM_TS_QUEUE
22 #define OPENLIBRARY_PALGORITHM_TS_QUEUE
27 #include <condition_variable>
31 #include "utils/optional.hpp"
74 std::unique_lock<std::mutex> lock(m_queue_mutex);
75 assert(m_queue.empty());
84 assert(m_stopped ==
false);
86 if (m_stopped ==
false)
88 std::unique_lock<std::mutex> lock(m_queue_mutex);
90 m_is_not_full.wait(lock, [&] {
return m_queue.size() < m_max_size; } );
91 m_queue.push_back(item);
92 m_is_not_empty.notify_one();
97 void push_front(
const T &item)
107 assert(m_stopped ==
false);
109 if (m_stopped ==
false)
111 std::unique_lock<std::mutex> lock(m_queue_mutex);
113 m_is_not_full.wait(lock, [&] {
return m_queue.size() < m_max_size; } );
114 m_queue.push_back(std::move(item));
115 m_is_not_empty.notify_one();
120 void push_back(T&& item)
122 push(std::move(item));
133 std::unique_lock<std::mutex> lock(m_queue_mutex);
138 if (m_queue.empty() ==
false)
140 result = std::move( *(m_queue.begin()) );
142 m_is_not_full.notify_one();
145 return std::move(result);
163 std::unique_lock<std::mutex> lock(m_queue_mutex);
168 if (status && m_queue.empty() ==
false)
170 result = std::move( *(m_queue.begin()) );
172 m_is_not_full.notify_one();
175 return std::move(result);
181 std::unique_lock<std::mutex> lock(m_queue_mutex);
182 const size_t result = m_queue.size();
189 std::unique_lock<std::mutex> lock(m_queue_mutex);
190 const bool result = m_queue.empty();
199 m_is_not_empty.notify_all();
205 std::unique_lock<std::mutex> lock(m_queue_mutex);
212 std::deque<T> m_queue;
213 std::condition_variable m_is_not_full;
214 std::condition_variable m_is_not_empty;
215 mutable std::mutex m_queue_mutex;
217 std::atomic<bool> m_stopped;
223 const bool condition = m_stopped ==
false && m_queue.empty();
227 m_is_not_empty.wait(lock, precond);
230 bool wait_for_data(std::unique_lock<std::mutex>& lock,
const std::chrono::milliseconds& timeout)
234 const bool condition = m_stopped ==
false && m_queue.empty();
238 const bool status = m_is_not_empty.wait_for(lock, timeout, precond);
TS_Queue(size_t max_size)
Definition: ts_queue.hpp:57
size_t size() const
Take objects count.
Definition: ts_queue.hpp:179
Definition: optional.hpp:27
void stop()
Release all threads waiting in TS_Queue::pop().
Definition: ts_queue.hpp:196
Optional< T > pop_for(const std::chrono::milliseconds &timeout)
Get data.
Definition: ts_queue.hpp:161
bool empty() const
Check if TS_Queue is empty.
Definition: ts_queue.hpp:187
Thread safe queue.
Definition: ts_queue.hpp:52
void push(T &&item)
Write data to TS_Queue.
Definition: ts_queue.hpp:105
virtual ~TS_Queue()
Destructor.
Definition: ts_queue.hpp:70
void push(const T &item)
Write data to TS_Queue.
Definition: ts_queue.hpp:82
Optional< T > pop()
Get data.
Definition: ts_queue.hpp:131
void wait_for_data()
Wait until data is available.
Definition: ts_queue.hpp:203