Skip to content

WIP (Not ready): STYLE: Replace once_flag of ThreadPoolGlobals with static local variable #4171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Modules/Core/Common/src/itkThreadPool.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ struct ThreadPoolGlobals
// To lock on the various internal variables.
std::mutex m_Mutex;

// To allow singleton creation of ThreadPool.
std::once_flag m_ThreadPoolOnceFlag;

// The singleton instance of ThreadPool.
ThreadPool::Pointer m_ThreadPoolInstance;

Expand Down Expand Up @@ -75,8 +72,8 @@ ThreadPool::GetInstance()
// initialized.
itkInitGlobalsMacro(PimplGlobals);

// Create a singleton ThreadPool.
std::call_once(m_PimplGlobals->m_ThreadPoolOnceFlag, []() {
// Create a ThreadPool singleton as part of the initialization of a static local variable, to ensure thread-safety.
[[maybe_unused]] static const volatile bool isCalledOnce = [] {
m_PimplGlobals->m_ThreadPoolInstance = ObjectFactory<Self>::Create();
if (m_PimplGlobals->m_ThreadPoolInstance.IsNull())
{
Expand All @@ -85,7 +82,8 @@ ThreadPool::GetInstance()
#if defined(ITK_USE_PTHREADS)
pthread_atfork(ThreadPool::PrepareForFork, ThreadPool::ResumeFromFork, ThreadPool::ResumeFromFork);
#endif
});
return true;
}();

return m_PimplGlobals->m_ThreadPoolInstance;
}
Expand All @@ -107,7 +105,7 @@ ThreadPool::SetDoNotWaitForThreads(bool doNotWaitForThreads)
ThreadPool::ThreadPool()
{
// m_PimplGlobals->m_Mutex not needed to be acquired here because construction only occurs via GetInstance which is
// protected by call_once.
// protected by the thread-safe initialization of a static local variable, `isCalledOnce`.

m_PimplGlobals->m_ThreadPoolInstance = this; // threads need this
m_PimplGlobals->m_ThreadPoolInstance->UnRegister(); // Remove extra reference
Expand Down