changeset 6760:6ebd2030e5a8

improved efficiency of restarting thread pool
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 11 May 2026 17:42:21 +0200
parents c5173fb5f2d2
children f75c39296c83
files OrthancFramework/Sources/MultiThreading/ThreadPool.cpp OrthancFramework/Sources/MultiThreading/ThreadPool.h
diffstat 2 files changed, 13 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp	Mon May 11 14:26:01 2026 +0200
+++ b/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp	Mon May 11 17:42:21 2026 +0200
@@ -133,7 +133,9 @@
       }
     }
 
-    workers_.join_all();
+    assert(workers_.get() != NULL);
+    workers_->join_all();
+    workers_.reset(NULL);
 
     // Cancel all the remaining tasks in the queue
     for (;;)
@@ -270,9 +272,12 @@
 
       state_ = State_Running;
 
+      assert(workers_.get() == NULL);
+      workers_.reset(new boost::thread_group);
+
       for (unsigned int i = 0; i < countThreads_; i++)
       {
-        workers_.create_thread(boost::bind(&ThreadPool::WorkerLoop, this));
+        workers_->create_thread(boost::bind(&ThreadPool::WorkerLoop, this));
       }
     }
     else
--- a/OrthancFramework/Sources/MultiThreading/ThreadPool.h	Mon May 11 14:26:01 2026 +0200
+++ b/OrthancFramework/Sources/MultiThreading/ThreadPool.h	Mon May 11 17:42:21 2026 +0200
@@ -44,12 +44,12 @@
 
     class Task;
 
-    SharedMessageQueue   queue_;
-    boost::thread_group  workers_;
-    boost::mutex         mutex_;
-    unsigned int         countThreads_;
-    State                state_;
-    unsigned int         dequeueTimeoutMilliseconds_;
+    SharedMessageQueue                    queue_;
+    std::unique_ptr<boost::thread_group>  workers_;
+    boost::mutex                          mutex_;
+    unsigned int                          countThreads_;
+    State                                 state_;
+    unsigned int                          dequeueTimeoutMilliseconds_;
 
     void StopInternal(bool throws);