From a268ca04e6a69796d23b68d2e47abcbd7647f27e Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Wed, 21 Jun 2023 23:33:16 -0500 Subject: [PATCH] Asynchronous threads should execute in thread pools owned by parents - Fixes #51 --- scwx-qt/source/scwx/qt/main/main_window.cpp | 11 +++- .../scwx/qt/manager/text_event_manager.cpp | 55 ++++++++++--------- scwx-qt/source/scwx/qt/map/map_widget.cpp | 44 ++++++++------- 3 files changed, 61 insertions(+), 49 deletions(-) diff --git a/scwx-qt/source/scwx/qt/main/main_window.cpp b/scwx-qt/source/scwx/qt/main/main_window.cpp index f3d8a4f4..23fd0785 100644 --- a/scwx-qt/source/scwx/qt/main/main_window.cpp +++ b/scwx-qt/source/scwx/qt/main/main_window.cpp @@ -28,8 +28,9 @@ #include #include #include -#include +#include +#include #include #include #include @@ -134,6 +135,8 @@ public: void UpdateRadarSite(); void UpdateVcp(); + boost::asio::thread_pool threadPool_ {1u}; + MainWindow* mainWindow_; QMapLibreGL::Settings settings_; map::MapProvider mapProvider_; @@ -429,7 +432,8 @@ void MainWindow::on_actionGitHubRepository_triggered() void MainWindow::on_actionCheckForUpdates_triggered() { - scwx::util::async( + boost::asio::post( + p->threadPool_, [this]() { if (!p->updateManager_->CheckForUpdates(main::kVersionString_)) @@ -544,7 +548,8 @@ void MainWindowImpl::AsyncSetup() // Check for updates if (generalSettings.update_notifications_enabled().GetValue()) { - scwx::util::async( + boost::asio::post( + threadPool_, [this]() { updateManager_->CheckForUpdates(main::kVersionString_); }); } } diff --git a/scwx-qt/source/scwx/qt/manager/text_event_manager.cpp b/scwx-qt/source/scwx/qt/manager/text_event_manager.cpp index 6bc8119c..d44d4fb0 100644 --- a/scwx-qt/source/scwx/qt/manager/text_event_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/text_event_manager.cpp @@ -3,12 +3,13 @@ #include #include #include -#include #include #include +#include #include +#include namespace scwx { @@ -28,19 +29,19 @@ class TextEventManager::Impl public: explicit Impl(TextEventManager* self) : self_ {self}, - refreshTimer_ {util::io_context()}, + refreshTimer_ {threadPool_}, refreshMutex_ {}, textEventMap_ {}, textEventMutex_ {}, warningsProvider_ {kDefaultWarningsProviderUrl} { - util::async( - [this]() - { - main::Application::WaitForInitialization(); - logger_->debug("Start Refresh"); - Refresh(); - }); + boost::asio::post(threadPool_, + [this]() + { + main::Application::WaitForInitialization(); + logger_->debug("Start Refresh"); + Refresh(); + }); } ~Impl() @@ -52,6 +53,8 @@ public: void HandleMessage(std::shared_ptr message); void Refresh(); + boost::asio::thread_pool threadPool_ {1u}; + TextEventManager* self_; boost::asio::steady_timer refreshTimer_; @@ -104,25 +107,25 @@ void TextEventManager::LoadFile(const std::string& filename) { logger_->debug("LoadFile: {}", filename); - util::async( - [=, this]() - { - awips::TextProductFile file; + boost::asio::post(p->threadPool_, + [=, this]() + { + awips::TextProductFile file; - // Load file - bool fileLoaded = file.LoadFile(filename); - if (!fileLoaded) - { - return; - } + // Load file + bool fileLoaded = file.LoadFile(filename); + if (!fileLoaded) + { + return; + } - // Process messages - auto messages = file.messages(); - for (auto& message : messages) - { - p->HandleMessage(message); - } - }); + // Process messages + auto messages = file.messages(); + for (auto& message : messages) + { + p->HandleMessage(message); + } + }); } void TextEventManager::Impl::HandleMessage( diff --git a/scwx-qt/source/scwx/qt/map/map_widget.cpp b/scwx-qt/source/scwx/qt/map/map_widget.cpp index c41c90bd..fc92c3fa 100644 --- a/scwx-qt/source/scwx/qt/map/map_widget.cpp +++ b/scwx-qt/source/scwx/qt/map/map_widget.cpp @@ -13,13 +13,14 @@ #include #include #include -#include #include #include #include #include +#include +#include #include #include #include @@ -123,6 +124,8 @@ public: common::Level2Product GetLevel2ProductOrDefault(const std::string& productName) const; + boost::asio::thread_pool threadPool_ {1u}; + boost::uuids::uuid uuid_; std::shared_ptr context_; @@ -939,7 +942,8 @@ void MapWidgetImpl::RadarProductManagerConnect() } // Load file - scwx::util::async( + boost::asio::post( + threadPool_, [=, this]() { if (group == common::RadarProductGroup::Level2) @@ -973,26 +977,26 @@ void MapWidgetImpl::RadarProductManagerDisconnect() void MapWidgetImpl::InitializeNewRadarProductView( const std::string& colorPalette) { - scwx::util::async( - [=, this]() - { - auto radarProductView = context_->radar_product_view(); + boost::asio::post(threadPool_, + [=, this]() + { + auto radarProductView = context_->radar_product_view(); - std::string colorTableFile = - manager::SettingsManager::palette_settings() - .palette(colorPalette) - .GetValue(); - if (!colorTableFile.empty()) - { - std::unique_ptr colorTableStream = - util::OpenFile(colorTableFile); - std::shared_ptr colorTable = - common::ColorTable::Load(*colorTableStream); - radarProductView->LoadColorTable(colorTable); - } + std::string colorTableFile = + manager::SettingsManager::palette_settings() + .palette(colorPalette) + .GetValue(); + if (!colorTableFile.empty()) + { + std::unique_ptr colorTableStream = + util::OpenFile(colorTableFile); + std::shared_ptr colorTable = + common::ColorTable::Load(*colorTableStream); + radarProductView->LoadColorTable(colorTable); + } - radarProductView->Initialize(); - }); + radarProductView->Initialize(); + }); if (map_ != nullptr) {