aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ThreadsafeQueue.h
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2020-04-21 15:51:31 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2020-04-21 15:51:31 +0200
commit43d5fa8e85dd013391a8eafc16350cc13b008389 (patch)
tree63b93aefd0428a6e4a3604d73f1ab076a601f3ad /lib/ThreadsafeQueue.h
parent0aa8e58e6763bda1d20246155e61b7cd54cdfc65 (diff)
downloadODR-SourceCompanion-43d5fa8e85dd013391a8eafc16350cc13b008389.tar.gz
ODR-SourceCompanion-43d5fa8e85dd013391a8eafc16350cc13b008389.tar.bz2
ODR-SourceCompanion-43d5fa8e85dd013391a8eafc16350cc13b008389.zip
Common 33a8362: EDI TCP output: handle disconnects
Diffstat (limited to 'lib/ThreadsafeQueue.h')
-rw-r--r--lib/ThreadsafeQueue.h23
1 files changed, 19 insertions, 4 deletions
diff --git a/lib/ThreadsafeQueue.h b/lib/ThreadsafeQueue.h
index 62f4c96..815dfe0 100644
--- a/lib/ThreadsafeQueue.h
+++ b/lib/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<std::mutex> 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<std::mutex> 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();