From f976e88359efd8c20740d9a77660f76320a5caaa Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sat, 18 May 2024 14:33:13 -0500 Subject: [PATCH] Properly manage QThread termination --- .../source/scwx/qt/manager/thread_manager.cpp | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/scwx-qt/source/scwx/qt/manager/thread_manager.cpp b/scwx-qt/source/scwx/qt/manager/thread_manager.cpp index 8b12edb0..78924434 100644 --- a/scwx-qt/source/scwx/qt/manager/thread_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/thread_manager.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -63,11 +64,24 @@ void ThreadManager::StopThreads() logger_->debug("Stopping threads"); - for (auto& thread : p->threadMap_) - { - thread.second->quit(); - thread.second->deleteLater(); - } + std::for_each(std::execution::par_unseq, + p->threadMap_.begin(), + p->threadMap_.end(), + [](auto& thread) + { + logger_->trace("Stopping thread: {}", thread.first); + + thread.second->quit(); + if (!thread.second->wait(5000)) + { + logger_->warn("Terminating thread: {}", thread.first); + + thread.second->terminate(); + thread.second->wait(); + } + + delete thread.second; + }); p->threadMap_.clear(); }