]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1599] fixed race on wait and disable in ThreadPoolQueue
authorRazvan Becheriu <razvan@isc.org>
Thu, 10 Dec 2020 20:52:58 +0000 (22:52 +0200)
committerRazvan Becheriu <razvan@isc.org>
Wed, 15 Nov 2023 06:36:55 +0000 (08:36 +0200)
src/lib/util/thread_pool.h

index eec91c7b97eef9bc55bda5b3453dfbff31c9684e..48d2042b180c778867ef83c17a4b0e19d3c9d71a 100644 (file)
@@ -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<std::thread>(&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<std::mutex> 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<std::mutex> 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 {