Skip to content
Permalink
Browse files

Store std::thread in worker thread class by value and explicitly star…

…t it up, and don't use shared_ptr in thread pools
  • Loading branch information...
CookiePLMonster committed Aug 10, 2019
1 parent 34109af commit 1a992bf023f386f8973ead747abe4f9726bf0a55
Showing with 17 additions and 16 deletions.
  1. +11 −10 ext/native/thread/threadpool.cpp
  2. +6 −6 ext/native/thread/threadpool.h
@@ -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<std::mutex> 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<void()> 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<void(int, int)> work, int start, int end) {
std::lock_guard<std::mutex> 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<LoopWorkerThread>());
auto workerPtr = make_unique<LoopWorkerThread>();
workerPtr->StartUp();
workers.push_back(std::move(workerPtr));
}
workersStarted = true;
}
@@ -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<void()> work);
// wait for a submitted work item to be completed
void WaitForCompletion();

protected:
WorkerThread(bool ignored) {}

std::unique_ptr<std::thread> 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 @@ class WorkerThread {

class LoopWorkerThread final : public WorkerThread {
public:
LoopWorkerThread();
LoopWorkerThread() = default;
void Process(std::function<void(int, int)> work, int start, int end);

private:
@@ -65,7 +65,7 @@ class ThreadPool {

private:
int numThreads_;
std::vector<std::shared_ptr<LoopWorkerThread>> workers;
std::vector<std::unique_ptr<LoopWorkerThread>> workers;
std::mutex mutex; // used to sequentialize loop execution

bool workersStarted = false;

0 comments on commit 1a992bf

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