From b1438d4fa31aa9e967f1f7e2d48f55ca371151d1 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 21 Apr 2020 14:12:50 +0200 Subject: Change EDI output to handle disconnects --- contrib/ThreadsafeQueue.h | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'contrib/ThreadsafeQueue.h') diff --git a/contrib/ThreadsafeQueue.h b/contrib/ThreadsafeQueue.h index 62f4c96..815dfe0 100644 --- a/contrib/ThreadsafeQueue.h +++ b/contrib/ThreadsafeQueue.h @@ -52,12 +52,21 @@ public: /* Push one element into the queue, and notify another thread that * might be waiting. * + * if max_size > 0 and the queue already contains at least max_size elements, + * the element gets discarded. + * * returns the new queue size. */ - size_t push(T const& val) + size_t push(T const& val, size_t max_size = 0) { std::unique_lock lock(the_mutex); - the_queue.push(val); + size_t queue_size_before = the_queue.size(); + if (max_size == 0) { + the_queue.push(val); + } + else if (queue_size_before < max_size) { + the_queue.push(val); + } size_t queue_size = the_queue.size(); lock.unlock(); @@ -66,10 +75,16 @@ public: return queue_size; } - size_t push(T&& val) + size_t push(T&& val, size_t max_size = 0) { std::unique_lock lock(the_mutex); - the_queue.emplace(std::move(val)); + size_t queue_size_before = the_queue.size(); + if (max_size == 0) { + the_queue.emplace(std::move(val)); + } + else if (queue_size_before < max_size) { + the_queue.emplace(std::move(val)); + } size_t queue_size = the_queue.size(); lock.unlock(); -- cgit v1.2.3