diff --git a/ext/native/thread/threadpool.cpp b/ext/native/thread/threadpool.cpp index 37a99f737d..d48fd6a994 100644 --- a/ext/native/thread/threadpool.cpp +++ b/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 guard(mutex); + active = false; + signal.notify_one(); + } thread->join(); } void WorkerThread::Process(std::function work) { - mutex.lock(); + std::lock_guard 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 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 doneGuard(doneMutex); jobsDone++; - doneMutex.unlock(); + done.notify_one(); } } }