1#ifndef OSMIUM_THREAD_QUEUE_HPP
2#define OSMIUM_THREAD_QUEUE_HPP
38#include <condition_variable>
45#ifdef OSMIUM_DEBUG_QUEUE_SIZE
78#ifdef OSMIUM_DEBUG_QUEUE_SIZE
80 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, std::string name =
"") :
113#ifdef OSMIUM_DEBUG_QUEUE_SIZE
130#ifdef OSMIUM_DEBUG_QUEUE_SIZE
132 std::cerr <<
"queue '" <<
m_name
134 <<
" had largest size " << m_largest_size
135 <<
" and was full " << m_full_counter
136 <<
" times in " << m_push_counter
137 <<
" push() calls and was empty " << m_empty_counter
138 <<
" times in " << m_pop_counter
153 constexpr const std::chrono::milliseconds max_wait{10};
154#ifdef OSMIUM_DEBUG_QUEUE_SIZE
159 std::unique_lock<std::mutex> lock{
m_mutex};
163#ifdef OSMIUM_DEBUG_QUEUE_SIZE
168 const std::lock_guard<std::mutex> lock{
m_mutex};
169 m_queue.push(std::move(value));
170#ifdef OSMIUM_DEBUG_QUEUE_SIZE
171 if (m_largest_size <
m_queue.size()) {
172 m_largest_size =
m_queue.size();
179#ifdef OSMIUM_DEBUG_QUEUE_SIZE
182 std::unique_lock<std::mutex> lock{
m_mutex};
183#ifdef OSMIUM_DEBUG_QUEUE_SIZE
192 value = std::move(
m_queue.front());
202#ifdef OSMIUM_DEBUG_QUEUE_SIZE
206 const std::lock_guard<std::mutex> lock{
m_mutex};
208#ifdef OSMIUM_DEBUG_QUEUE_SIZE
213 value = std::move(
m_queue.front());
223 const std::lock_guard<std::mutex> lock{
m_mutex};
228 const std::lock_guard<std::mutex> lock{
m_mutex};
238 const std::lock_guard<std::mutex> lock{
m_mutex};
bool try_pop(T &value)
Definition: queue.hpp:201
std::mutex m_mutex
Definition: queue.hpp:66
Queue & operator=(const Queue &)=delete
Queue & operator=(Queue &&)=delete
bool empty() const
Definition: queue.hpp:222
bool in_use() const noexcept
Definition: queue.hpp:232
void push(T value)
Definition: queue.hpp:149
void wait_and_pop(T &value)
Definition: queue.hpp:178
std::atomic< bool > m_in_use
Definition: queue.hpp:76
Queue(const Queue &)=delete
std::condition_variable m_space_available
Used to signal producers when queue is not full.
Definition: queue.hpp:74
Queue(std::size_t max_size=0, std::string name="")
Definition: queue.hpp:109
std::size_t size() const
Definition: queue.hpp:227
std::queue< T > m_queue
Definition: queue.hpp:68
const std::size_t m_max_size
Definition: queue.hpp:61
std::condition_variable m_data_available
Used to signal consumers when data is available in the queue.
Definition: queue.hpp:71
void shutdown()
Definition: queue.hpp:236
const std::string m_name
Name of this queue (for debugging only).
Definition: queue.hpp:64
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:555