Skip to content

Commit

Permalink
Cleanup mutex usage in worker threads
Browse files Browse the repository at this point in the history
  • Loading branch information
CookiePLMonster committed Aug 11, 2019
1 parent 400a6f3 commit 34109af
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions ext/native/thread/threadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
}
}
}
Expand Down Expand Up @@ -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();
}
}
}
Expand Down

0 comments on commit 34109af

Please sign in to comment.