Skip to content

Commit

Permalink
ENH: Use std::once_flag for GlobalSingletonIndexInitializer
Browse files Browse the repository at this point in the history
  • Loading branch information
seanm authored and dzenanz committed Nov 9, 2021
1 parent 1933058 commit bd8adf9
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Modules/Core/Common/src/itkSingleton.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
*=========================================================================*/
#include "itkSingleton.h"

#include <mutex>

namespace
{
std::once_flag globalSingletonOnceFlag;

// This ensures that m_GlobalSingletonIndex has been initialized once the library
// has been loaded. In some cases, this call will perform the initialization.
// In other cases, static initializers like the IO factory initialization code
Expand Down Expand Up @@ -50,13 +53,14 @@ class GlobalSingletonIndexInitializer
static SingletonIndex *
GetGlobalSingletonIndex()
{
if (m_GlobalSingletonIndex == nullptr)
{
std::call_once(globalSingletonOnceFlag, []() {
m_GlobalSingletonIndex = new SingletonIndex;

// To avoid being optimized out. The compiler does not like this
// statement at a higher scope.
Unused(initializedGlobalSingletonIndex);
}
});

return m_GlobalSingletonIndex;
}

Expand Down

0 comments on commit bd8adf9

Please sign in to comment.