From 1a992bf023f386f8973ead747abe4f9726bf0a55 Mon Sep 17 00:00:00 2001 From: Silent Date: Sat, 10 Aug 2019 20:25:16 +0200 Subject: [PATCH] Store std::thread in worker thread class by value and explicitly start it up, and don't use shared_ptr in thread pools --- ext/native/thread/threadpool.cpp | 21 +++++++++++---------- ext/native/thread/threadpool.h | 12 ++++++------ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/ext/native/thread/threadpool.cpp b/ext/native/thread/threadpool.cpp index d48fd6a994..cdf98fd1de 100644 --- a/ext/native/thread/threadpool.cpp +++ b/ext/native/thread/threadpool.cpp @@ -1,20 +1,23 @@ #include "base/logging.h" #include "thread/threadpool.h" #include "thread/threadutil.h" +#include "Common/MakeUnique.h" ///////////////////////////// WorkerThread -WorkerThread::WorkerThread() { - thread.reset(new std::thread(std::bind(&WorkerThread::WorkFunc, this))); -} - WorkerThread::~WorkerThread() { { std::lock_guard guard(mutex); active = false; signal.notify_one(); } - thread->join(); + if (thread.joinable()) { + thread.join(); + } +} + +void WorkerThread::StartUp() { + thread = std::thread(std::bind(&WorkerThread::WorkFunc, this)); } void WorkerThread::Process(std::function work) { @@ -50,10 +53,6 @@ void WorkerThread::WorkFunc() { } } -LoopWorkerThread::LoopWorkerThread() : WorkerThread(true) { - thread.reset(new std::thread(std::bind(&LoopWorkerThread::WorkFunc, this))); -} - void LoopWorkerThread::Process(std::function work, int start, int end) { std::lock_guard guard(mutex); work_ = std::move(work); @@ -99,7 +98,9 @@ ThreadPool::ThreadPool(int numThreads) { void ThreadPool::StartWorkers() { if (!workersStarted) { for(int i = 0; i < numThreads_; ++i) { - workers.push_back(std::make_shared()); + auto workerPtr = make_unique(); + workerPtr->StartUp(); + workers.push_back(std::move(workerPtr)); } workersStarted = true; } diff --git a/ext/native/thread/threadpool.h b/ext/native/thread/threadpool.h index 581302d7d2..2064b31c28 100644 --- a/ext/native/thread/threadpool.h +++ b/ext/native/thread/threadpool.h @@ -12,18 +12,18 @@ // Only handles a single item of work at a time. class WorkerThread { public: - WorkerThread(); + WorkerThread() = default; virtual ~WorkerThread(); + void StartUp(); + // submit a new work item void Process(std::function work); // wait for a submitted work item to be completed void WaitForCompletion(); protected: - WorkerThread(bool ignored) {} - - std::unique_ptr thread; // the worker thread + std::thread thread; // the worker thread std::condition_variable signal; // used to signal new work std::condition_variable done; // used to signal work completion std::mutex mutex, doneMutex; // associated with each respective condition variable @@ -41,7 +41,7 @@ private: class LoopWorkerThread final : public WorkerThread { public: - LoopWorkerThread(); + LoopWorkerThread() = default; void Process(std::function work, int start, int end); private: @@ -65,7 +65,7 @@ public: private: int numThreads_; - std::vector> workers; + std::vector> workers; std::mutex mutex; // used to sequentialize loop execution bool workersStarted = false;