changeset 6528:5541a6b13500

avoid possible duplicate thread names
author Alain Mazy <am@orthanc.team>
date Tue, 02 Dec 2025 12:03:16 +0100
parents ef77a536195b
children e0979326ac53
files OrthancServer/Sources/ServerJobs/ArchiveJob.cpp OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp
diffstat 2 files changed, 16 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Tue Dec 02 12:02:19 2025 +0100
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Tue Dec 02 12:03:16 2025 +0100
@@ -56,6 +56,8 @@
 static const char* const KEY_ARCHIVE_SIZE = "ArchiveSize";
 static const char* const KEY_TRANSCODE = "Transcode";
 
+static boost::mutex loaderThreadsCounterMutex;
+static uint32_t loaderThreadsCounter = 0;
 
 namespace Orthanc
 {
@@ -251,8 +253,11 @@
 
     static void PreloaderWorkerThread(ThreadedInstanceLoader* that)
     {
-      static uint16_t threadCounter = 0;
-      Logging::SetCurrentThreadName(std::string("ARCH-LOAD-") + boost::lexical_cast<std::string>(threadCounter++));
+      {
+        boost::mutex::scoped_lock lock(loaderThreadsCounterMutex);
+        Logging::SetCurrentThreadName(std::string("ARCH-LOAD-") + boost::lexical_cast<std::string>(loaderThreadsCounter++));
+        loaderThreadsCounter %= 1000000;
+      }
 
       LOG(INFO) << "Loader thread has started";
 
--- a/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp	Tue Dec 02 12:02:19 2025 +0100
+++ b/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp	Tue Dec 02 12:03:16 2025 +0100
@@ -31,6 +31,10 @@
 #include <boost/lexical_cast.hpp>
 #include <cassert>
 
+static boost::mutex instanceWorkerThreadsCounterMutex;
+static uint32_t instanceWorkerThreadsCounter = 0;
+
+
 namespace Orthanc
 {
   static const char* EXIT_WORKER_MESSAGE = "exit";
@@ -244,9 +248,11 @@
 
   void ThreadedSetOfInstancesJob::InstanceWorkerThread(ThreadedSetOfInstancesJob* that)
   {
-    static uint16_t threadCounter = 0;
-    Logging::SetCurrentThreadName(std::string("JOB-INS-WORK-") + boost::lexical_cast<std::string>(threadCounter++));
-    threadCounter %= 1000;
+    {
+      boost::mutex::scoped_lock lock(instanceWorkerThreadsCounterMutex);
+      Logging::SetCurrentThreadName(std::string("JOB-INS-WORK-") + boost::lexical_cast<std::string>(instanceWorkerThreadsCounter++));
+      instanceWorkerThreadsCounter %= 1000;
+    }
 
     while (true)
     {