1 #ifndef OSMIUM_THREAD_QUEUE_HPP 2 #define OSMIUM_THREAD_QUEUE_HPP 37 #include <condition_variable> 45 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 76 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 77 size_t m_largest_size;
81 std::atomic<int> m_push_counter;
85 std::atomic<int> m_full_counter;
91 std::atomic<int> m_pop_counter;
95 std::atomic<int> m_empty_counter;
107 explicit Queue(
size_t max_size = 0,
const std::string& name =
"") :
108 m_max_size(max_size),
113 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
125 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 126 std::cerr <<
"queue '" << m_name <<
"' with max_size=" << m_max_size <<
" had largest size " << m_largest_size <<
" and was full " << m_full_counter <<
" times in " << m_push_counter <<
" push() calls and was empty " << m_empty_counter <<
" times in " << m_pop_counter <<
" pop() calls\n";
135 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 139 while (
size() >= m_max_size) {
141 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 146 std::lock_guard<std::mutex> lock(m_mutex);
147 m_queue.push(std::move(value));
148 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 149 if (m_largest_size < m_queue.size()) {
150 m_largest_size = m_queue.size();
153 m_data_available.notify_one();
157 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 160 std::unique_lock<std::mutex> lock(m_mutex);
161 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 162 if (m_queue.empty()) {
166 m_data_available.wait(lock, [
this] {
167 return !m_queue.empty();
169 if (!m_queue.empty()) {
170 value = std::move(m_queue.front());
176 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 179 std::lock_guard<std::mutex> lock(m_mutex);
180 if (m_queue.empty()) {
181 #ifdef OSMIUM_DEBUG_QUEUE_SIZE 186 value = std::move(m_queue.front());
192 std::lock_guard<std::mutex> lock(m_mutex);
193 return m_queue.empty();
197 std::lock_guard<std::mutex> lock(m_mutex);
198 return m_queue.size();
207 #endif // OSMIUM_THREAD_QUEUE_HPP size_t size() const
Definition: queue.hpp:196
std::mutex m_mutex
Definition: queue.hpp:69
~Queue()
Definition: queue.hpp:124
std::queue< T > m_queue
Definition: queue.hpp:71
const std::string m_name
Name of this queue (for debugging only).
Definition: queue.hpp:67
const size_t m_max_size
Definition: queue.hpp:64
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
bool try_pop(T &value)
Definition: queue.hpp:175
static const std::chrono::milliseconds full_queue_sleep_duration
Definition: queue.hpp:54
void wait_and_pop(T &value)
Definition: queue.hpp:156
void push(T value)
Definition: queue.hpp:134
bool empty() const
Definition: queue.hpp:191
Queue(size_t max_size=0, const std::string &name="")
Definition: queue.hpp:107
std::condition_variable m_data_available
Used to signal readers when data is available in the queue.
Definition: queue.hpp:74