From: Razvan Becheriu Date: Thu, 10 Dec 2020 20:52:58 +0000 (+0200) Subject: [#1599] fixed race on wait and disable in ThreadPoolQueue X-Git-Tag: Kea-2.5.4~43 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4e5554b4cf85f088766cfc02cc8e913abbd99c80;p=thirdparty%2Fkea.git [#1599] fixed race on wait and disable in ThreadPoolQueue --- diff --git a/src/lib/util/thread_pool.h b/src/lib/util/thread_pool.h index eec91c7b97..48d2042b18 100644 --- a/src/lib/util/thread_pool.h +++ b/src/lib/util/thread_pool.h @@ -185,7 +185,7 @@ private: sigaddset(&sset, SIGHUP); sigaddset(&sset, SIGTERM); pthread_sigmask(SIG_BLOCK, &sset, &osset); - queue_.enable(thread_count); + queue_.enable(); try { for (uint32_t i = 0; i < thread_count; ++i) { threads_.push_back(boost::make_shared(&ThreadPool::run, this)); @@ -253,6 +253,12 @@ private: clear(); } + /// @brief register thread so that it can be taken into account + void registerThread() { + std::lock_guard lock(mutex_); + ++working_; + } + /// @brief set maximum number of work items in the queue /// /// @return the maximum size (0 means unlimited) @@ -422,12 +428,9 @@ private: /// @brief enable the queue /// /// Sets the queue state to 'enabled' - /// - /// @param thread_count number of working threads - void enable(uint32_t thread_count) { + void enable() { std::lock_guard lock(mutex_); enabled_ = true; - working_ = thread_count; } /// @brief disable the queue @@ -488,7 +491,12 @@ private: /// @brief run function of each thread void run() { - for (bool work = true; work; work = queue_.enabled()) { + bool register_thread = true; + while (queue_.enabled()) { + if (register_thread) { + queue_.registerThread(); + register_thread = false; + } WorkItemPtr item = queue_.pop(); if (item) { try {