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/vcp.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 <QFileDialog>
#include <QMessageBox>
@ -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_); });
}
}

View file

@ -3,12 +3,13 @@
#include <scwx/awips/text_product_file.hpp>
#include <scwx/provider/warnings_provider.hpp>
#include <scwx/util/logger.hpp>
#include <scwx/util/threads.hpp>
#include <shared_mutex>
#include <unordered_map>
#include <boost/asio/post.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/thread_pool.hpp>
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<awips::TextProductMessage> 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(

View file

@ -13,13 +13,14 @@
#include <scwx/qt/util/file.hpp>
#include <scwx/qt/view/radar_product_view_factory.hpp>
#include <scwx/util/logger.hpp>
#include <scwx/util/threads.hpp>
#include <scwx/util/time.hpp>
#include <regex>
#include <backends/imgui_impl_opengl3.h>
#include <backends/imgui_impl_qt.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/uuid/random_generator.hpp>
#include <fmt/format.h>
#include <imgui.h>
@ -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<MapContext> 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<std::istream> colorTableStream =
util::OpenFile(colorTableFile);
std::shared_ptr<common::ColorTable> colorTable =
common::ColorTable::Load(*colorTableStream);
radarProductView->LoadColorTable(colorTable);
}
std::string colorTableFile =
manager::SettingsManager::palette_settings()
.palette(colorPalette)
.GetValue();
if (!colorTableFile.empty())
{
std::unique_ptr<std::istream> colorTableStream =
util::OpenFile(colorTableFile);
std::shared_ptr<common::ColorTable> colorTable =
common::ColorTable::Load(*colorTableStream);
radarProductView->LoadColorTable(colorTable);
}
radarProductView->Initialize();
});
radarProductView->Initialize();
});
if (map_ != nullptr)
{