Skip to content

Commit

Permalink
BUG: Use atomic bool to fix race found by thread sanitizer
Browse files Browse the repository at this point in the history
  • Loading branch information
seanm authored and dzenanz committed Sep 21, 2023
1 parent 04100e6 commit c3a8eab
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions Modules/Core/Common/src/itkObjectFactoryBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "itksys/SystemTools.hxx"
#include <cstring>
#include <algorithm>
#include <atomic>

namespace
{
Expand Down Expand Up @@ -120,10 +121,10 @@ class ObjectFactoryBase::ObjectFactoryBasePrivate : public LightObject

ObjectFactoryBasePrivate() = default;

FactoryListType m_RegisteredFactories{};
FactoryListType m_InternalFactories{};
bool m_Initialized{ false };
bool m_StrictVersionChecking{ false };
FactoryListType m_RegisteredFactories{};
FactoryListType m_InternalFactories{};
std::atomic<bool> m_Initialized{ false };
bool m_StrictVersionChecking{ false };
};

auto
Expand Down Expand Up @@ -236,9 +237,9 @@ ObjectFactoryBase::Initialize()
{
itkInitGlobalsMacro(PimplGlobals);

if (!m_PimplGlobals->m_Initialized)
// Atomically set m_Initialized to true. If it was false before, enter the if.
if (!m_PimplGlobals->m_Initialized.exchange(true))
{
m_PimplGlobals->m_Initialized = true;
ObjectFactoryBase::InitializeFactoryList();
ObjectFactoryBase::RegisterInternal();
#if defined(ITK_DYNAMIC_LOADING) && !defined(ITK_WRAPPING)
Expand Down

0 comments on commit c3a8eab

Please sign in to comment.