From b11d17cf826b76033265323e7f89280de620bd8c Mon Sep 17 00:00:00 2001 From: Ferenc Date: Tue, 3 Jun 2025 23:09:27 +0200 Subject: [PATCH] - ensure capacity atleast 1 - capacity setter notifies notFullCond to avoid deadlock on waiting for put - docstring --- include/mqtt/thread_queue.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/include/mqtt/thread_queue.h b/include/mqtt/thread_queue.h index d05e4734..de6b5713 100644 --- a/include/mqtt/thread_queue.h +++ b/include/mqtt/thread_queue.h @@ -149,14 +149,18 @@ class thread_queue return cap_; } /** - * Sets the capacity of the queue. - * Note that the capacity can be set to a value smaller than the current - * size of the queue. In that event, all calls to put() will block until - * a sufficient number + * Sets the capacity of the queue. + * Note that the capacity can be set to a value smaller than the current + * size of the queue. In that event, all calls to put() will block until + * a sufficient number of items are removed from the queue. */ void capacity(size_type cap) { - guard g{lock_}; - cap_ = cap; + unique_guard g(lock_); + cap_ = std::max(cap, 1); + if(cap_ > que_.size()) { + g.unlock(); + notFullCond_.notify_all(); + } } /** * Gets the number of items in the queue.