From b36cd3db4bd6650a76215d721720ae8c56359956 Mon Sep 17 00:00:00 2001 From: Maksim Derbasov Date: Wed, 2 Aug 2023 20:54:59 +0900 Subject: [PATCH 1/2] Graceful thread shutdown in GUI --- gui/threadhandler.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gui/threadhandler.cpp b/gui/threadhandler.cpp index 43faf2aea27..af6da876e14 100644 --- a/gui/threadhandler.cpp +++ b/gui/threadhandler.cpp @@ -148,7 +148,8 @@ void ThreadHandler::setThreadCount(const int count) void ThreadHandler::removeThreads() { for (CheckThread* thread : mThreads) { - thread->terminate(); + thread->quit(); + thread->wait(); disconnect(thread, &CheckThread::done, this, &ThreadHandler::threadDone); disconnect(thread, &CheckThread::fileChecked, From fbc3a79c4408d6455376fffbad0a2afbe2a27398 Mon Sep 17 00:00:00 2001 From: Maksim Derbasov Date: Wed, 2 Aug 2023 22:25:19 +0900 Subject: [PATCH 2/2] Review iteration --- gui/checkthread.h | 6 ++++-- gui/threadhandler.cpp | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/gui/checkthread.h b/gui/checkthread.h index 9a1fea3e628..d74ec2514a9 100644 --- a/gui/checkthread.h +++ b/gui/checkthread.h @@ -24,6 +24,8 @@ #include "importproject.h" #include "suppressions.h" +#include + #include #include #include @@ -118,9 +120,9 @@ class CheckThread : public QThread { }; /** - * @brief Thread's current execution state. + * @brief Thread's current execution state. Can be changed from outside */ - State mState = Ready; + std::atomic mState{Ready}; ThreadResult &mResult; /** diff --git a/gui/threadhandler.cpp b/gui/threadhandler.cpp index af6da876e14..6e99afe0488 100644 --- a/gui/threadhandler.cpp +++ b/gui/threadhandler.cpp @@ -148,8 +148,10 @@ void ThreadHandler::setThreadCount(const int count) void ThreadHandler::removeThreads() { for (CheckThread* thread : mThreads) { - thread->quit(); - thread->wait(); + if (thread->isRunning()) { + thread->terminate(); + thread->wait(); + } disconnect(thread, &CheckThread::done, this, &ThreadHandler::threadDone); disconnect(thread, &CheckThread::fileChecked,