Properly manage QThread termination

This commit is contained in:
Dan Paulat 2024-05-18 14:33:13 -05:00
parent 8f7076cd09
commit f976e88359

View file

@ -1,6 +1,7 @@
#include <scwx/qt/manager/thread_manager.hpp> #include <scwx/qt/manager/thread_manager.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
#include <execution>
#include <mutex> #include <mutex>
#include <boost/unordered/unordered_flat_map.hpp> #include <boost/unordered/unordered_flat_map.hpp>
@ -63,11 +64,24 @@ void ThreadManager::StopThreads()
logger_->debug("Stopping threads"); logger_->debug("Stopping threads");
for (auto& thread : p->threadMap_) std::for_each(std::execution::par_unseq,
{ p->threadMap_.begin(),
thread.second->quit(); p->threadMap_.end(),
thread.second->deleteLater(); [](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(); p->threadMap_.clear();
} }