Skip to content

Commit

Permalink
STYLE: Use lock_guard<mutex> in PlatformMultiThreader classes
Browse files Browse the repository at this point in the history
Following C++ Core Guidelines, April 13, 2023, "Use RAII, never plain `lock()`/`unlock()`"
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cp20-use-raii-never-plain-lockunlock

Follow-up to pull request InsightSoftwareConsortium#3898
commit a744aef
"STYLE: Use `lock_guard<mutex>` in Logger classes and GPUImageDataManager"
  • Loading branch information
N-Dekker committed Aug 26, 2023
1 parent 9765f46 commit 91481a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
16 changes: 7 additions & 9 deletions Modules/Core/Common/src/itkPlatformMultiThreaderPosix.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,20 @@ PlatformMultiThreader::SpawnThread(ThreadFunctionType f, void * UserData)
{
ThreadIdType id = 0;

while (id < ITK_MAX_THREADS)
for (; id < ITK_MAX_THREADS; ++id)
{
if (!m_SpawnedThreadActiveFlagLock[id])
{
m_SpawnedThreadActiveFlagLock[id] = std::make_shared<std::mutex>();
}
m_SpawnedThreadActiveFlagLock[id]->lock();
const std::lock_guard<std::mutex> lockGuard(*m_SpawnedThreadActiveFlagLock[id]);

if (m_SpawnedThreadActiveFlag[id] == 0)
{
// We've got a useable thread id, so grab it
m_SpawnedThreadActiveFlag[id] = 1;
m_SpawnedThreadActiveFlagLock[id]->unlock();
break;
}
m_SpawnedThreadActiveFlagLock[id]->unlock();

++id;
}

if (id >= ITK_MAX_THREADS)
Expand Down Expand Up @@ -163,9 +160,10 @@ PlatformMultiThreader::TerminateThread(ThreadIdType WorkUnitID)
return;
}

m_SpawnedThreadActiveFlagLock[WorkUnitID]->lock();
m_SpawnedThreadActiveFlag[WorkUnitID] = 0;
m_SpawnedThreadActiveFlagLock[WorkUnitID]->unlock();
{
const std::lock_guard<std::mutex> lockGuard(*m_SpawnedThreadActiveFlagLock[WorkUnitID]);
m_SpawnedThreadActiveFlag[WorkUnitID] = 0;
}

pthread_join(m_SpawnedThreadProcessID[WorkUnitID], nullptr);

Expand Down
9 changes: 3 additions & 6 deletions Modules/Core/Common/src/itkPlatformMultiThreaderWindows.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,20 @@ PlatformMultiThreader::SpawnThread(ThreadFunctionType f, void * UserData)
{
ThreadIdType id = 0;

while (id < ITK_MAX_THREADS)
for (; id < ITK_MAX_THREADS; ++id)
{
if (!m_SpawnedThreadActiveFlagLock[id])
{
m_SpawnedThreadActiveFlagLock[id] = std::make_shared<std::mutex>();
}
m_SpawnedThreadActiveFlagLock[id]->lock();
const std::lock_guard<std::mutex> lockGuard(*m_SpawnedThreadActiveFlagLock[WorkUnitID]);

if (m_SpawnedThreadActiveFlag[id] == 0)
{
// We've got a useable thread id, so grab it
m_SpawnedThreadActiveFlag[id] = 1;
m_SpawnedThreadActiveFlagLock[id]->unlock();
break;
}
m_SpawnedThreadActiveFlagLock[id]->unlock();

++id;
}

if (id >= ITK_MAX_THREADS)
Expand Down

0 comments on commit 91481a8

Please sign in to comment.