Skip to content

Commit 8e2709d

Browse files
committed
STYLE: Replace once_flag of ThreadPoolGlobals with static local variable
A static local `bool` variable is more _lightweight_ than a `std::once_flag` data member, while its (static) initialization just as thread-safe as `std::call_once`.
1 parent c6690b9 commit 8e2709d

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

Modules/Core/Common/src/itkThreadPool.cxx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ struct ThreadPoolGlobals
4040
// To lock on the various internal variables.
4141
std::mutex m_Mutex;
4242

43-
// To allow singleton creation of ThreadPool.
44-
std::once_flag m_ThreadPoolOnceFlag;
45-
4643
// The singleton instance of ThreadPool.
4744
ThreadPool::Pointer m_ThreadPoolInstance;
4845

@@ -75,8 +72,8 @@ ThreadPool::GetInstance()
7572
// initialized.
7673
itkInitGlobalsMacro(PimplGlobals);
7774

78-
// Create a singleton ThreadPool.
79-
std::call_once(m_PimplGlobals->m_ThreadPoolOnceFlag, []() {
75+
// Create a ThreadPool singleton as part of the initialization of a static local variable, to ensure thread-safety.
76+
[[maybe_unused]] static const bool isCalledOnce = [] {
8077
m_PimplGlobals->m_ThreadPoolInstance = ObjectFactory<Self>::Create();
8178
if (m_PimplGlobals->m_ThreadPoolInstance.IsNull())
8279
{
@@ -85,7 +82,8 @@ ThreadPool::GetInstance()
8582
#if defined(ITK_USE_PTHREADS)
8683
pthread_atfork(ThreadPool::PrepareForFork, ThreadPool::ResumeFromFork, ThreadPool::ResumeFromFork);
8784
#endif
88-
});
85+
return true;
86+
}();
8987

9088
return m_PimplGlobals->m_ThreadPoolInstance;
9189
}

0 commit comments

Comments
 (0)