Skip to content

Commit

Permalink
[MultiThreading] Avoid Static Initialization Order Fiasco (#4307)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxbilger authored Nov 29, 2023
1 parent 0602271 commit 8e1dca0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
namespace multithreading
{

sofa::type::vector<ParallelImplementationsRegistry::Implementation> ParallelImplementationsRegistry::s_implementations;
sofa::type::vector<ParallelImplementationsRegistry::Implementation>& getStaticImplementations()
{
static sofa::type::vector<ParallelImplementationsRegistry::Implementation> s_implementations;
return s_implementations;
}

bool ParallelImplementationsRegistry::addEquivalentImplementations(
const std::string& sequentialImplementation, const std::string& parallelImplementation)
Expand All @@ -34,9 +38,9 @@ bool ParallelImplementationsRegistry::addEquivalentImplementations(

const auto it = findParallelImplementationImpl(sequentialImplementation);

if (it == s_implementations.end())
if (it == getStaticImplementations().end())
{
s_implementations.push_back(implementation);
getStaticImplementations().push_back(implementation);
return true;
}

Expand All @@ -62,7 +66,7 @@ std::string ParallelImplementationsRegistry::findParallelImplementation(
{
const auto it = findParallelImplementationImpl(sequentialImplementation);

if (it != s_implementations.end())
if (it != getImplementations().end())
{
return it->parallel;
}
Expand All @@ -72,14 +76,14 @@ std::string ParallelImplementationsRegistry::findParallelImplementation(
const sofa::type::vector<ParallelImplementationsRegistry::Implementation>&
ParallelImplementationsRegistry::getImplementations()
{
return s_implementations;
return getStaticImplementations();
}

sofa::type::vector<ParallelImplementationsRegistry::Implementation>::const_iterator
ParallelImplementationsRegistry::findParallelImplementationImpl(
const std::string& sequentialImplementation)
{
return std::find_if(s_implementations.begin(), s_implementations.end(),
return std::find_if(getStaticImplementations().begin(), getStaticImplementations().end(),
[&sequentialImplementation](const Implementation& i)
{
return i.sequential == sequentialImplementation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class SOFA_MULTITHREADING_PLUGIN_API ParallelImplementationsRegistry
static const sofa::type::vector<Implementation>& getImplementations();

private:
static sofa::type::vector<Implementation> s_implementations;

static sofa::type::vector<Implementation>::const_iterator findParallelImplementationImpl(const std::string& sequentialImplementation);
};
Expand Down

0 comments on commit 8e1dca0

Please sign in to comment.