Skip to content

Commit

Permalink
STYLE: Replace std::unique_lock with std::lock_guard in ThreadPool
Browse files Browse the repository at this point in the history
A `std::lock_guard` is more lightweight than a `std::unique_lock`, so for
simple use cases, `std::lock_guard` appears preferable.
  • Loading branch information
N-Dekker committed Aug 23, 2023
1 parent 434eacc commit 4fe8b13
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Modules/Core/Common/include/itkThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ auto result = pool->AddWork([](int param) { return param; }, 7);

std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(this->GetMutex());
const std::lock_guard<std::mutex> lockGuard(this->GetMutex());
m_WorkQueue.emplace_back([task]() { (*task)(); });
}
m_Condition.notify_one();
Expand All @@ -107,7 +107,7 @@ auto result = pool->AddWork([](int param) { return param; }, 7);
ThreadIdType
GetMaximumNumberOfThreads() const
{
std::unique_lock<std::mutex> lock(this->GetMutex());
const std::lock_guard<std::mutex> lockGuard(this->GetMutex());
return static_cast<ThreadIdType>(m_Threads.size());
}

Expand Down
6 changes: 3 additions & 3 deletions Modules/Core/Common/src/itkThreadPool.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ ThreadPool::ThreadPool()
void
ThreadPool::AddThreads(ThreadIdType count)
{
std::unique_lock<std::mutex> mutexHolder(m_PimplGlobals->m_Mutex);
const std::lock_guard<std::mutex> lockGuard(m_PimplGlobals->m_Mutex);
m_Threads.reserve(m_Threads.size() + count);
for (ThreadIdType i = 0; i < count; ++i)
{
Expand All @@ -139,7 +139,7 @@ ThreadPool::GetMutex() const
int
ThreadPool::GetNumberOfCurrentlyIdleThreads() const
{
std::unique_lock<std::mutex> mutexHolder(m_PimplGlobals->m_Mutex);
const std::lock_guard<std::mutex> lockGuard(m_PimplGlobals->m_Mutex);
return static_cast<int>(m_Threads.size()) - static_cast<int>(m_WorkQueue.size()); // lousy approximation
}

Expand All @@ -148,7 +148,7 @@ ThreadPool::CleanUp()
{
bool shouldNotify;
{
std::unique_lock<std::mutex> mutexHolder(m_PimplGlobals->m_Mutex);
const std::lock_guard<std::mutex> lockGuard(m_PimplGlobals->m_Mutex);

this->m_Stopping = true;

Expand Down

0 comments on commit 4fe8b13

Please sign in to comment.