Update Thread.cpp

Fixed a bug that caused a race between ~Thread and Thread::Runner receiving a new job from the threadpool
This commit is contained in:
2025-07-13 13:23:04 -04:00
parent e2b847665f
commit 518e73a036

View File

@@ -5,13 +5,19 @@
using namespace MultiThreading;
Thread::~Thread() {
if (current_task)
current_task->WaitComplete();
stop = true;
std::shared_ptr<TaskBase> task;
{
std::lock_guard<std::mutex> lock(mtx);
task = current_task;
}
if (task)
task->WaitComplete();
stop = true;
cv.notify_one();
Join();
// Thread exits gracefully.
}
bool Thread::SetTask(std::shared_ptr<TaskBase> task) {
@@ -43,9 +49,10 @@ void Thread::Runner() {
if (stop)
break;
auto task = current_task;
lock.unlock();
current_task->Run();
task->Run();
lock.lock();