fix: replaced manual .lock()/.unlock() calls with std::scoped_lock

This commit is contained in:
Sara Gerretsen 2025-09-23 13:40:51 +02:00
parent 622fc2cb40
commit 8eb5f3e3b8

View file

@ -1,6 +1,7 @@
#include "thread_pool.h" #include "thread_pool.h"
#include <algorithm> #include <algorithm>
#include <thread> #include <thread>
#include <utility>
namespace threading { namespace threading {
ThreadPool::ThreadPool() { ThreadPool::ThreadPool() {
@ -12,11 +13,10 @@ ThreadPool::ThreadPool() {
} }
ThreadPool::~ThreadPool() { ThreadPool::~ThreadPool() {
this->lock.lock(); { std::scoped_lock lock{ this->lock };
this->shutdown = true; this->shutdown = true;
this->threadNotifier.notify_all(); this->threadNotifier.notify_all();
this->lock.unlock(); }
for (std::thread &thread : this->threads) { for (std::thread &thread : this->threads) {
thread.join(); thread.join();
} }
@ -27,10 +27,9 @@ size_t ThreadPool::GetThreadCount() {
} }
void ThreadPool::ScheduleTask(TaskFunc fn) { void ThreadPool::ScheduleTask(TaskFunc fn) {
this->lock.lock(); std::scoped_lock lock{ this->lock };
this->taskQueue.emplace(std::move(fn)); this->taskQueue.emplace(std::move(fn));
this->threadNotifier.notify_one(); this->threadNotifier.notify_one();
this->lock.unlock();
} }
void ThreadPool::ThreadFn() { void ThreadPool::ThreadFn() {