From 4fe8b13a8e604bb232bb3ce7b4be045d94b81d44 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Wed, 23 Aug 2023 13:50:33 +0200 Subject: [PATCH] STYLE: Replace `std::unique_lock` with `std::lock_guard` in ThreadPool A `std::lock_guard` is more lightweight than a `std::unique_lock`, so for simple use cases, `std::lock_guard` appears preferable. --- Modules/Core/Common/include/itkThreadPool.h | 4 ++-- Modules/Core/Common/src/itkThreadPool.cxx | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Modules/Core/Common/include/itkThreadPool.h b/Modules/Core/Common/include/itkThreadPool.h index ca0be924e8b..821fbd257d8 100644 --- a/Modules/Core/Common/include/itkThreadPool.h +++ b/Modules/Core/Common/include/itkThreadPool.h @@ -93,7 +93,7 @@ auto result = pool->AddWork([](int param) { return param; }, 7); std::future res = task->get_future(); { - std::unique_lock lock(this->GetMutex()); + const std::lock_guard lockGuard(this->GetMutex()); m_WorkQueue.emplace_back([task]() { (*task)(); }); } m_Condition.notify_one(); @@ -107,7 +107,7 @@ auto result = pool->AddWork([](int param) { return param; }, 7); ThreadIdType GetMaximumNumberOfThreads() const { - std::unique_lock lock(this->GetMutex()); + const std::lock_guard lockGuard(this->GetMutex()); return static_cast(m_Threads.size()); } diff --git a/Modules/Core/Common/src/itkThreadPool.cxx b/Modules/Core/Common/src/itkThreadPool.cxx index dfcfc6164ca..57065659fa3 100644 --- a/Modules/Core/Common/src/itkThreadPool.cxx +++ b/Modules/Core/Common/src/itkThreadPool.cxx @@ -122,7 +122,7 @@ ThreadPool::ThreadPool() void ThreadPool::AddThreads(ThreadIdType count) { - std::unique_lock mutexHolder(m_PimplGlobals->m_Mutex); + const std::lock_guard lockGuard(m_PimplGlobals->m_Mutex); m_Threads.reserve(m_Threads.size() + count); for (ThreadIdType i = 0; i < count; ++i) { @@ -139,7 +139,7 @@ ThreadPool::GetMutex() const int ThreadPool::GetNumberOfCurrentlyIdleThreads() const { - std::unique_lock mutexHolder(m_PimplGlobals->m_Mutex); + const std::lock_guard lockGuard(m_PimplGlobals->m_Mutex); return static_cast(m_Threads.size()) - static_cast(m_WorkQueue.size()); // lousy approximation } @@ -148,7 +148,7 @@ ThreadPool::CleanUp() { bool shouldNotify; { - std::unique_lock mutexHolder(m_PimplGlobals->m_Mutex); + const std::lock_guard lockGuard(m_PimplGlobals->m_Mutex); this->m_Stopping = true;