Add thread manager for QThreads

This commit is contained in:
Dan Paulat 2024-05-18 11:08:48 -05:00
parent 2efdd1b046
commit 8f7076cd09
4 changed files with 126 additions and 0 deletions

View file

@ -99,6 +99,7 @@ set(HDR_MANAGER source/scwx/qt/manager/alert_manager.hpp
source/scwx/qt/manager/resource_manager.hpp source/scwx/qt/manager/resource_manager.hpp
source/scwx/qt/manager/settings_manager.hpp source/scwx/qt/manager/settings_manager.hpp
source/scwx/qt/manager/text_event_manager.hpp source/scwx/qt/manager/text_event_manager.hpp
source/scwx/qt/manager/thread_manager.hpp
source/scwx/qt/manager/timeline_manager.hpp source/scwx/qt/manager/timeline_manager.hpp
source/scwx/qt/manager/update_manager.hpp) source/scwx/qt/manager/update_manager.hpp)
set(SRC_MANAGER source/scwx/qt/manager/alert_manager.cpp set(SRC_MANAGER source/scwx/qt/manager/alert_manager.cpp
@ -113,6 +114,7 @@ set(SRC_MANAGER source/scwx/qt/manager/alert_manager.cpp
source/scwx/qt/manager/resource_manager.cpp source/scwx/qt/manager/resource_manager.cpp
source/scwx/qt/manager/settings_manager.cpp source/scwx/qt/manager/settings_manager.cpp
source/scwx/qt/manager/text_event_manager.cpp source/scwx/qt/manager/text_event_manager.cpp
source/scwx/qt/manager/thread_manager.cpp
source/scwx/qt/manager/timeline_manager.cpp source/scwx/qt/manager/timeline_manager.cpp
source/scwx/qt/manager/update_manager.cpp) source/scwx/qt/manager/update_manager.cpp)
set(HDR_MAP source/scwx/qt/map/alert_layer.hpp set(HDR_MAP source/scwx/qt/map/alert_layer.hpp

View file

@ -7,6 +7,7 @@
#include <scwx/qt/manager/radar_product_manager.hpp> #include <scwx/qt/manager/radar_product_manager.hpp>
#include <scwx/qt/manager/resource_manager.hpp> #include <scwx/qt/manager/resource_manager.hpp>
#include <scwx/qt/manager/settings_manager.hpp> #include <scwx/qt/manager/settings_manager.hpp>
#include <scwx/qt/manager/thread_manager.hpp>
#include <scwx/qt/settings/general_settings.hpp> #include <scwx/qt/settings/general_settings.hpp>
#include <scwx/qt/types/qt_types.hpp> #include <scwx/qt/types/qt_types.hpp>
#include <scwx/qt/ui/setup/setup_wizard.hpp> #include <scwx/qt/ui/setup/setup_wizard.hpp>
@ -129,6 +130,9 @@ int main(int argc, char* argv[])
// Deinitialize application // Deinitialize application
scwx::qt::manager::RadarProductManager::Cleanup(); scwx::qt::manager::RadarProductManager::Cleanup();
// Stop Qt Threads
scwx::qt::manager::ThreadManager::Instance().StopThreads();
// Gracefully stop the io_context main loop // Gracefully stop the io_context main loop
work.reset(); work.reset();
threadPool.join(); threadPool.join();

View file

@ -0,0 +1,83 @@
#include <scwx/qt/manager/thread_manager.hpp>
#include <scwx/util/logger.hpp>
#include <mutex>
#include <boost/unordered/unordered_flat_map.hpp>
#include <QThread>
namespace scwx
{
namespace qt
{
namespace manager
{
static const std::string logPrefix_ = "scwx::qt::manager::thread_manager";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
class ThreadManager::Impl
{
public:
explicit Impl() {}
~Impl() {}
std::mutex mutex_ {};
boost::unordered_flat_map<std::string, QThread*> threadMap_ {};
};
ThreadManager::ThreadManager() : p(std::make_unique<Impl>()) {}
ThreadManager::~ThreadManager() = default;
QThread* ThreadManager::thread(const std::string& id, bool autoStart)
{
std::unique_lock lock {p->mutex_};
QThread* thread = nullptr;
auto it = p->threadMap_.find(id);
if (it != p->threadMap_.cend())
{
thread = it->second;
}
if (thread == nullptr)
{
logger_->debug("Creating thread: {}", id);
thread = new QThread(this);
p->threadMap_.insert_or_assign(id, thread);
if (autoStart)
{
thread->start();
}
}
return thread;
}
void ThreadManager::StopThreads()
{
std::unique_lock lock {p->mutex_};
logger_->debug("Stopping threads");
for (auto& thread : p->threadMap_)
{
thread.second->quit();
thread.second->deleteLater();
}
p->threadMap_.clear();
}
ThreadManager& ThreadManager::Instance()
{
static ThreadManager threadManager_ {};
return threadManager_;
}
} // namespace manager
} // namespace qt
} // namespace scwx

View file

@ -0,0 +1,37 @@
#pragma once
#include <memory>
#include <QObject>
#include <QThread>
namespace scwx
{
namespace qt
{
namespace manager
{
class ThreadManager : public QObject
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(ThreadManager)
public:
explicit ThreadManager();
~ThreadManager();
QThread* thread(const std::string& id, bool autoStart = true);
void StopThreads();
static ThreadManager& Instance();
private:
class Impl;
std::unique_ptr<Impl> p;
};
} // namespace manager
} // namespace qt
} // namespace scwx