diff --git a/Common/ThreadPools.cpp b/Common/ThreadPools.cpp index 53bfe12dfa..9af420cf37 100644 --- a/Common/ThreadPools.cpp +++ b/Common/ThreadPools.cpp @@ -1,18 +1,16 @@ #include "ThreadPools.h" #include "../Core/Config.h" +#include "Common/MakeUnique.h" -std::shared_ptr GlobalThreadPool::pool; -bool GlobalThreadPool::initialized = false; +std::unique_ptr GlobalThreadPool::pool; +std::once_flag GlobalThreadPool::init_flag; void GlobalThreadPool::Loop(const std::function& loop, int lower, int upper) { - Inititialize(); + std::call_once(init_flag, Inititialize); pool->ParallelLoop(loop, lower, upper); } void GlobalThreadPool::Inititialize() { - if(!initialized) { - pool = std::make_shared(g_Config.iNumWorkerThreads); - initialized = true; - } + pool = make_unique(g_Config.iNumWorkerThreads); } diff --git a/Common/ThreadPools.h b/Common/ThreadPools.h index 2ae46c2c00..e04303a9d2 100644 --- a/Common/ThreadPools.h +++ b/Common/ThreadPools.h @@ -9,7 +9,7 @@ public: static void Loop(const std::function& loop, int lower, int upper); private: - static std::shared_ptr pool; - static bool initialized; + static std::unique_ptr pool; + static std::once_flag init_flag; static void Inititialize(); };