1 #ifndef OSMIUM_THREAD_QUEUE_HPP 2 #define OSMIUM_THREAD_QUEUE_HPP 37 #include <condition_variable> 44 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 53 static const std::chrono::milliseconds
max_wait{10};
78 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 79 std::size_t m_largest_size;
83 std::atomic<int> m_push_counter;
87 std::atomic<int> m_full_counter;
93 std::atomic<int> m_pop_counter;
97 std::atomic<int> m_empty_counter;
109 explicit Queue(std::size_t max_size = 0,
const std::string& name =
"") :
110 m_max_size(max_size),
116 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
128 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 129 std::cerr <<
"queue '" << m_name
130 <<
"' with max_size=" << m_max_size
131 <<
" had largest size " << m_largest_size
132 <<
" and was full " << m_full_counter
133 <<
" times in " << m_push_counter
134 <<
" push() calls and was empty " << m_empty_counter
135 <<
" times in " << m_pop_counter
145 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 149 while (
size() >= m_max_size) {
150 std::unique_lock<std::mutex> lock{m_mutex};
151 m_space_available.wait_for(lock,
max_wait, [
this] {
154 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 159 std::lock_guard<std::mutex> lock{m_mutex};
160 m_queue.push(std::move(value));
161 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 162 if (m_largest_size < m_queue.size()) {
163 m_largest_size = m_queue.size();
166 m_data_available.notify_one();
170 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 173 std::unique_lock<std::mutex> lock{m_mutex};
174 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 175 if (m_queue.empty()) {
179 m_data_available.wait(lock, [
this] {
180 return !m_queue.empty();
182 if (!m_queue.empty()) {
183 value = std::move(m_queue.front());
187 m_space_available.notify_one();
193 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 197 std::lock_guard<std::mutex> lock{m_mutex};
198 if (m_queue.empty()) {
199 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 204 value = std::move(m_queue.front());
208 m_space_available.notify_one();
214 std::lock_guard<std::mutex> lock{m_mutex};
215 return m_queue.empty();
219 std::lock_guard<std::mutex> lock{m_mutex};
220 return m_queue.size();
229 #endif // OSMIUM_THREAD_QUEUE_HPP
std::mutex m_mutex
Definition: queue.hpp:68
~Queue()
Definition: queue.hpp:127
static const std::chrono::milliseconds max_wait
Definition: queue.hpp:53
std::queue< T > m_queue
Definition: queue.hpp:70
const std::string m_name
Name of this queue (for debugging only).
Definition: queue.hpp:66
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
std::condition_variable m_space_available
Used to signal producers when queue is not full.
Definition: queue.hpp:76
bool try_pop(T &value)
Definition: queue.hpp:192
Queue(std::size_t max_size=0, const std::string &name="")
Definition: queue.hpp:109
void wait_and_pop(T &value)
Definition: queue.hpp:169
void push(T value)
Definition: queue.hpp:144
bool empty() const
Definition: queue.hpp:213
std::size_t size() const
Definition: queue.hpp:218
const std::size_t m_max_size
Definition: queue.hpp:63
std::condition_variable m_data_available
Used to signal consumers when data is available in the queue.
Definition: queue.hpp:73