From 86a887d7b09358cb6a535b001aff7a8cfc73d781 Mon Sep 17 00:00:00 2001 From: Silent Date: Sat, 10 Aug 2019 20:55:52 +0200 Subject: [PATCH] Init GlobalThreadPool in a thread safe manner --- Common/ThreadPools.cpp | 12 +++++------- Common/ThreadPools.h | 4 ++-- 2 files changed, 7 insertions(+), 9 deletions(-) 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(); };