Asynchronous threads should execute in thread pools owned by parents

- Fixes #51
This commit is contained in:
Dan Paulat 2023-06-21 23:33:16 -05:00
parent 6000abdeb3
commit a268ca04e6
3 changed files with 61 additions and 49 deletions

View file

@ -28,8 +28,9 @@
#include <scwx/common/products.hpp> #include <scwx/common/products.hpp>
#include <scwx/common/vcp.hpp> #include <scwx/common/vcp.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
#include <scwx/util/threads.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/thread_pool.hpp>
#include <QDesktopServices> #include <QDesktopServices>
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
@ -134,6 +135,8 @@ public:
void UpdateRadarSite(); void UpdateRadarSite();
void UpdateVcp(); void UpdateVcp();
boost::asio::thread_pool threadPool_ {1u};
MainWindow* mainWindow_; MainWindow* mainWindow_;
QMapLibreGL::Settings settings_; QMapLibreGL::Settings settings_;
map::MapProvider mapProvider_; map::MapProvider mapProvider_;
@ -429,7 +432,8 @@ void MainWindow::on_actionGitHubRepository_triggered()
void MainWindow::on_actionCheckForUpdates_triggered() void MainWindow::on_actionCheckForUpdates_triggered()
{ {
scwx::util::async( boost::asio::post(
p->threadPool_,
[this]() [this]()
{ {
if (!p->updateManager_->CheckForUpdates(main::kVersionString_)) if (!p->updateManager_->CheckForUpdates(main::kVersionString_))
@ -544,7 +548,8 @@ void MainWindowImpl::AsyncSetup()
// Check for updates // Check for updates
if (generalSettings.update_notifications_enabled().GetValue()) if (generalSettings.update_notifications_enabled().GetValue())
{ {
scwx::util::async( boost::asio::post(
threadPool_,
[this]() { updateManager_->CheckForUpdates(main::kVersionString_); }); [this]() { updateManager_->CheckForUpdates(main::kVersionString_); });
} }
} }

View file

@ -3,12 +3,13 @@
#include <scwx/awips/text_product_file.hpp> #include <scwx/awips/text_product_file.hpp>
#include <scwx/provider/warnings_provider.hpp> #include <scwx/provider/warnings_provider.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
#include <scwx/util/threads.hpp>
#include <shared_mutex> #include <shared_mutex>
#include <unordered_map> #include <unordered_map>
#include <boost/asio/post.hpp>
#include <boost/asio/steady_timer.hpp> #include <boost/asio/steady_timer.hpp>
#include <boost/asio/thread_pool.hpp>
namespace scwx namespace scwx
{ {
@ -28,19 +29,19 @@ class TextEventManager::Impl
public: public:
explicit Impl(TextEventManager* self) : explicit Impl(TextEventManager* self) :
self_ {self}, self_ {self},
refreshTimer_ {util::io_context()}, refreshTimer_ {threadPool_},
refreshMutex_ {}, refreshMutex_ {},
textEventMap_ {}, textEventMap_ {},
textEventMutex_ {}, textEventMutex_ {},
warningsProvider_ {kDefaultWarningsProviderUrl} warningsProvider_ {kDefaultWarningsProviderUrl}
{ {
util::async( boost::asio::post(threadPool_,
[this]() [this]()
{ {
main::Application::WaitForInitialization(); main::Application::WaitForInitialization();
logger_->debug("Start Refresh"); logger_->debug("Start Refresh");
Refresh(); Refresh();
}); });
} }
~Impl() ~Impl()
@ -52,6 +53,8 @@ public:
void HandleMessage(std::shared_ptr<awips::TextProductMessage> message); void HandleMessage(std::shared_ptr<awips::TextProductMessage> message);
void Refresh(); void Refresh();
boost::asio::thread_pool threadPool_ {1u};
TextEventManager* self_; TextEventManager* self_;
boost::asio::steady_timer refreshTimer_; boost::asio::steady_timer refreshTimer_;
@ -104,25 +107,25 @@ void TextEventManager::LoadFile(const std::string& filename)
{ {
logger_->debug("LoadFile: {}", filename); logger_->debug("LoadFile: {}", filename);
util::async( boost::asio::post(p->threadPool_,
[=, this]() [=, this]()
{ {
awips::TextProductFile file; awips::TextProductFile file;
// Load file // Load file
bool fileLoaded = file.LoadFile(filename); bool fileLoaded = file.LoadFile(filename);
if (!fileLoaded) if (!fileLoaded)
{ {
return; return;
} }
// Process messages // Process messages
auto messages = file.messages(); auto messages = file.messages();
for (auto& message : messages) for (auto& message : messages)
{ {
p->HandleMessage(message); p->HandleMessage(message);
} }
}); });
} }
void TextEventManager::Impl::HandleMessage( void TextEventManager::Impl::HandleMessage(

View file

@ -13,13 +13,14 @@
#include <scwx/qt/util/file.hpp> #include <scwx/qt/util/file.hpp>
#include <scwx/qt/view/radar_product_view_factory.hpp> #include <scwx/qt/view/radar_product_view_factory.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
#include <scwx/util/threads.hpp>
#include <scwx/util/time.hpp> #include <scwx/util/time.hpp>
#include <regex> #include <regex>
#include <backends/imgui_impl_opengl3.h> #include <backends/imgui_impl_opengl3.h>
#include <backends/imgui_impl_qt.hpp> #include <backends/imgui_impl_qt.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/uuid/random_generator.hpp> #include <boost/uuid/random_generator.hpp>
#include <fmt/format.h> #include <fmt/format.h>
#include <imgui.h> #include <imgui.h>
@ -123,6 +124,8 @@ public:
common::Level2Product common::Level2Product
GetLevel2ProductOrDefault(const std::string& productName) const; GetLevel2ProductOrDefault(const std::string& productName) const;
boost::asio::thread_pool threadPool_ {1u};
boost::uuids::uuid uuid_; boost::uuids::uuid uuid_;
std::shared_ptr<MapContext> context_; std::shared_ptr<MapContext> context_;
@ -939,7 +942,8 @@ void MapWidgetImpl::RadarProductManagerConnect()
} }
// Load file // Load file
scwx::util::async( boost::asio::post(
threadPool_,
[=, this]() [=, this]()
{ {
if (group == common::RadarProductGroup::Level2) if (group == common::RadarProductGroup::Level2)
@ -973,26 +977,26 @@ void MapWidgetImpl::RadarProductManagerDisconnect()
void MapWidgetImpl::InitializeNewRadarProductView( void MapWidgetImpl::InitializeNewRadarProductView(
const std::string& colorPalette) const std::string& colorPalette)
{ {
scwx::util::async( boost::asio::post(threadPool_,
[=, this]() [=, this]()
{ {
auto radarProductView = context_->radar_product_view(); auto radarProductView = context_->radar_product_view();
std::string colorTableFile = std::string colorTableFile =
manager::SettingsManager::palette_settings() manager::SettingsManager::palette_settings()
.palette(colorPalette) .palette(colorPalette)
.GetValue(); .GetValue();
if (!colorTableFile.empty()) if (!colorTableFile.empty())
{ {
std::unique_ptr<std::istream> colorTableStream = std::unique_ptr<std::istream> colorTableStream =
util::OpenFile(colorTableFile); util::OpenFile(colorTableFile);
std::shared_ptr<common::ColorTable> colorTable = std::shared_ptr<common::ColorTable> colorTable =
common::ColorTable::Load(*colorTableStream); common::ColorTable::Load(*colorTableStream);
radarProductView->LoadColorTable(colorTable); radarProductView->LoadColorTable(colorTable);
} }
radarProductView->Initialize(); radarProductView->Initialize();
}); });
if (map_ != nullptr) if (map_ != nullptr)
{ {