Skip to content
Permalink
Browse files

Init GlobalThreadPool in a thread safe manner

  • Loading branch information...
CookiePLMonster committed Aug 10, 2019
1 parent 4da4e6a commit 86a887d7b09358cb6a535b001aff7a8cfc73d781
Showing with 7 additions and 9 deletions.
  1. +5 −7 Common/ThreadPools.cpp
  2. +2 −2 Common/ThreadPools.h
@@ -1,18 +1,16 @@
#include "ThreadPools.h"

#include "../Core/Config.h"
#include "Common/MakeUnique.h"

std::shared_ptr<ThreadPool> GlobalThreadPool::pool;
bool GlobalThreadPool::initialized = false;
std::unique_ptr<ThreadPool> GlobalThreadPool::pool;
std::once_flag GlobalThreadPool::init_flag;

void GlobalThreadPool::Loop(const std::function<void(int,int)>& 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<ThreadPool>(g_Config.iNumWorkerThreads);
initialized = true;
}
pool = make_unique<ThreadPool>(g_Config.iNumWorkerThreads);
}
@@ -9,7 +9,7 @@ class GlobalThreadPool {
static void Loop(const std::function<void(int,int)>& loop, int lower, int upper);

private:
static std::shared_ptr<ThreadPool> pool;
static bool initialized;
static std::unique_ptr<ThreadPool> pool;
static std::once_flag init_flag;
static void Inititialize();
};

0 comments on commit 86a887d

Please sign in to comment.
You can’t perform that action at this time.