From ce4921e2be8768f917927672c197b731e51c973e Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Mon, 20 Nov 2023 12:02:22 +0100 Subject: [PATCH] [MultiThreading] Avoid Static Initialization Order Fiasco --- .../ParallelImplementationsRegistry.cpp | 16 ++++++++++------ .../ParallelImplementationsRegistry.h | 1 - 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/applications/plugins/MultiThreading/src/MultiThreading/ParallelImplementationsRegistry.cpp b/applications/plugins/MultiThreading/src/MultiThreading/ParallelImplementationsRegistry.cpp index e201063e013..78e33a5bb23 100644 --- a/applications/plugins/MultiThreading/src/MultiThreading/ParallelImplementationsRegistry.cpp +++ b/applications/plugins/MultiThreading/src/MultiThreading/ParallelImplementationsRegistry.cpp @@ -25,7 +25,11 @@ namespace multithreading { -sofa::type::vector ParallelImplementationsRegistry::s_implementations; +sofa::type::vector& getStaticImplementations() +{ + static sofa::type::vector s_implementations; + return s_implementations; +} bool ParallelImplementationsRegistry::addEquivalentImplementations( const std::string& sequentialImplementation, const std::string& parallelImplementation) @@ -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; } @@ -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; } @@ -72,14 +76,14 @@ std::string ParallelImplementationsRegistry::findParallelImplementation( const sofa::type::vector& ParallelImplementationsRegistry::getImplementations() { - return s_implementations; + return getStaticImplementations(); } sofa::type::vector::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; diff --git a/applications/plugins/MultiThreading/src/MultiThreading/ParallelImplementationsRegistry.h b/applications/plugins/MultiThreading/src/MultiThreading/ParallelImplementationsRegistry.h index 37cd573edb7..f7c6d90ee0b 100644 --- a/applications/plugins/MultiThreading/src/MultiThreading/ParallelImplementationsRegistry.h +++ b/applications/plugins/MultiThreading/src/MultiThreading/ParallelImplementationsRegistry.h @@ -44,7 +44,6 @@ class SOFA_MULTITHREADING_PLUGIN_API ParallelImplementationsRegistry static const sofa::type::vector& getImplementations(); private: - static sofa::type::vector s_implementations; static sofa::type::vector::const_iterator findParallelImplementationImpl(const std::string& sequentialImplementation); };