diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp index 14040fd..e4943a0 100644 --- a/src/thread_pool.cpp +++ b/src/thread_pool.cpp @@ -1,6 +1,7 @@ #include "thread_pool.h" #include #include +#include namespace threading { ThreadPool::ThreadPool() { @@ -12,11 +13,10 @@ ThreadPool::ThreadPool() { } ThreadPool::~ThreadPool() { - this->lock.lock(); - this->shutdown = true; - this->threadNotifier.notify_all(); - this->lock.unlock(); - + { std::scoped_lock lock{ this->lock }; + this->shutdown = true; + this->threadNotifier.notify_all(); + } for (std::thread &thread : this->threads) { thread.join(); } @@ -27,10 +27,9 @@ size_t ThreadPool::GetThreadCount() { } void ThreadPool::ScheduleTask(TaskFunc fn) { - this->lock.lock(); + std::scoped_lock lock{ this->lock }; this->taskQueue.emplace(std::move(fn)); this->threadNotifier.notify_one(); - this->lock.unlock(); } void ThreadPool::ThreadFn() {