Skip to content
Permalink
Browse files

Cleanup mutex usage in worker threads

  • Loading branch information...
CookiePLMonster committed Aug 10, 2019
1 parent 400a6f3 commit 34109afb2d686d81a7a35c5d31b1900ba50e48bf
Showing with 12 additions and 12 deletions.
  1. +12 −12 ext/native/thread/threadpool.cpp
@@ -9,19 +9,19 @@ WorkerThread::WorkerThread() {
}

WorkerThread::~WorkerThread() {
mutex.lock();
active = false;
signal.notify_one();
mutex.unlock();
{
std::lock_guard<std::mutex> guard(mutex);
active = false;
signal.notify_one();
}
thread->join();
}

void WorkerThread::Process(std::function<void()> work) {
mutex.lock();
std::lock_guard<std::mutex> guard(mutex);
work_ = std::move(work);
jobsTarget = jobsDone + 1;
signal.notify_one();
mutex.unlock();
}

void WorkerThread::WaitForCompletion() {
@@ -42,10 +42,10 @@ void WorkerThread::WorkFunc() {
}
if (active) {
work_();
doneMutex.lock();
done.notify_one();

std::lock_guard<std::mutex> doneGuard(doneMutex);
jobsDone++;
doneMutex.unlock();
done.notify_one();
}
}
}
@@ -74,10 +74,10 @@ void LoopWorkerThread::WorkFunc() {
}
if (active) {
work_(start_, end_);
doneMutex.lock();
done.notify_one();

std::lock_guard<std::mutex> doneGuard(doneMutex);
jobsDone++;
doneMutex.unlock();
done.notify_one();
}
}
}

0 comments on commit 34109af

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