From 23b89ddb32355f0949f5326fa967cc58bd00fbb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 13 Jun 2014 23:33:10 +0200 Subject: [PATCH] Cap threads at 8 to avoid problems with too many threads on CPUs that misreport core number --- thread/threadpool.cpp | 24 +++++++++++++++++------- thread/threadpool.h | 2 +- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/thread/threadpool.cpp b/thread/threadpool.cpp index 2b14500df8..b89447d084 100644 --- a/thread/threadpool.cpp +++ b/thread/threadpool.cpp @@ -1,3 +1,4 @@ +#include "base/logging.h" #include "threadpool.h" ///////////////////////////// WorkerThread @@ -73,12 +74,21 @@ void LoopWorkerThread::WorkFunc() { ///////////////////////////// ThreadPool -ThreadPool::ThreadPool(int numThreads) : numThreads(numThreads), workersStarted(false) { +ThreadPool::ThreadPool(int numThreads) : workersStarted(false) { + if (numThreads <= 0) { + numThreads_ = 1; + ILOG("ThreadPool: Bad number of threads %i", numThreads); + } else if (numThreads > 8) { + ILOG("ThreadPool: Capping number of threads to 8 (was %i)", numThreads); + numThreads_ = 8; + } else { + numThreads_ = numThreads; + } } void ThreadPool::StartWorkers() { - if(!workersStarted) { - for(int i=0; i()); } workersStarted = true; @@ -87,21 +97,21 @@ void ThreadPool::StartWorkers() { void ThreadPool::ParallelLoop(const std::function &loop, int lower, int upper) { int range = upper - lower; - if (range >= numThreads * 2) { // don't parallelize tiny loops (this could be better, maybe add optional parameter that estimates work per iteration) + if (range >= numThreads_ * 2) { // don't parallelize tiny loops (this could be better, maybe add optional parameter that estimates work per iteration) lock_guard guard(mutex); StartWorkers(); // could do slightly better load balancing for the generic case, // but doesn't matter since all our loops are power of 2 - int chunk = range / numThreads; + int chunk = range / numThreads_; int s = lower; - for (int i = 0; i < numThreads - 1; ++i) { + for (int i = 0; i < numThreads_ - 1; ++i) { workers[i]->Process(loop, s, s+chunk); s+=chunk; } // This is the final chunk. loop(s, upper); - for (int i = 0; i < numThreads - 1; ++i) { + for (int i = 0; i < numThreads_ - 1; ++i) { workers[i]->WaitForCompletion(); } } else { diff --git a/thread/threadpool.h b/thread/threadpool.h index c24c30caa7..7647f64d25 100644 --- a/thread/threadpool.h +++ b/thread/threadpool.h @@ -60,7 +60,7 @@ public: void ParallelLoop(const std::function &loop, int lower, int upper); private: - const int numThreads; + int numThreads_; std::vector> workers; ::recursive_mutex mutex; // used to sequentialize loop execution