From 57625b9680cc7f2832a833fc24d23d738c52f7b2 Mon Sep 17 00:00:00 2001 From: AdenKoperczak Date: Sun, 6 Oct 2024 12:15:40 -0400 Subject: [PATCH] Add async code to marker_manager --- .../source/scwx/qt/manager/marker_manager.cpp | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/scwx-qt/source/scwx/qt/manager/marker_manager.cpp b/scwx-qt/source/scwx/qt/manager/marker_manager.cpp index 15dc0509..116f1c3b 100644 --- a/scwx-qt/source/scwx/qt/manager/marker_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/marker_manager.cpp @@ -10,6 +10,8 @@ #include #include +#include +#include namespace scwx { @@ -31,13 +33,15 @@ public: class MarkerRecord; explicit Impl(MarkerManager* self) : self_ {self} {} - ~Impl() {} + ~Impl() { threadPool_.join(); } std::string markerSettingsPath_ {}; std::vector> markerRecords_ {}; MarkerManager* self_; + boost::asio::thread_pool threadPool_ {1u}; + void InitializeMarkerSettings(); void ReadMarkerSettings(); void WriteMarkerSettings(); @@ -166,19 +170,23 @@ MarkerManager::Impl::GetMarkerByName(const std::string& name) MarkerManager::MarkerManager() : p(std::make_unique(this)) { - // TODO THREADING? - try - { - p->InitializeMarkerSettings(); - // Read Marker settings on startup - // main::Application::WaitForInitialization(); - p->ReadMarkerSettings(); - } - catch (const std::exception& ex) - { - logger_->error(ex.what()); - } + boost::asio::post(p->threadPool_, + [this]() + { + try + { + p->InitializeMarkerSettings(); + + // Read Marker settings on startup + main::Application::WaitForInitialization(); + p->ReadMarkerSettings(); + } + catch (const std::exception& ex) + { + logger_->error(ex.what()); + } + }); } MarkerManager::~MarkerManager() @@ -264,6 +272,9 @@ void MarkerManager::move_marker(size_t from, size_t to) std::shared_ptr MarkerManager::Instance() { static std::weak_ptr markerManagerReference_ {}; + static std::mutex instanceMutex_ {}; + + std::unique_lock lock(instanceMutex_); std::shared_ptr markerManager = markerManagerReference_.lock();