From f480720665932a58cff0c18b5f43148207a8067f Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Tue, 3 Dec 2024 10:08:19 -0600 Subject: [PATCH] refactor --- .../librepertory/include/utils/polling.hpp | 4 +- repertory/librepertory/src/utils/polling.cpp | 56 ++++++++++--------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/repertory/librepertory/include/utils/polling.hpp b/repertory/librepertory/include/utils/polling.hpp index 5cce7133..7e582fc0 100644 --- a/repertory/librepertory/include/utils/polling.hpp +++ b/repertory/librepertory/include/utils/polling.hpp @@ -60,12 +60,10 @@ public: private: app_config *config_{nullptr}; - std::unique_ptr high_frequency_thread_; + std::array, 3U> frequency_threads_; std::unordered_map items_; - std::unique_ptr low_frequency_thread_; std::mutex mutex_; std::condition_variable notify_; - std::unique_ptr second_frequency_thread_; std::mutex start_stop_mutex_; stop_type stop_requested_{false}; diff --git a/repertory/librepertory/src/utils/polling.cpp b/repertory/librepertory/src/utils/polling.cpp index 49386d32..fd13fdce 100644 --- a/repertory/librepertory/src/utils/polling.cpp +++ b/repertory/librepertory/src/utils/polling.cpp @@ -80,36 +80,41 @@ void polling::set_callback(const polling_item &item) { void polling::start(app_config *config) { mutex_lock lock(start_stop_mutex_); - if (high_frequency_thread_) { + if (frequency_threads_.at(0U)) { return; } event_system::instance().raise("polling"); config_ = config; stop_requested_ = false; - high_frequency_thread_ = std::make_unique([this]() -> void { - this->frequency_thread( - [this]() -> std::uint32_t { - return config_->get_high_frequency_interval_secs(); - }, - frequency::high); - }); - low_frequency_thread_ = std::make_unique([this]() -> void { - this->frequency_thread( - [this]() -> std::uint32_t { - return config_->get_low_frequency_interval_secs(); - }, - frequency::low); - }); - second_frequency_thread_ = std::make_unique([this]() -> void { - this->frequency_thread([]() -> std::uint32_t { return 1U; }, - frequency::second); - }); + + auto idx{0U}; + frequency_threads_.at(idx++) = + std::make_unique([this]() -> void { + this->frequency_thread( + [this]() -> std::uint32_t { + return config_->get_high_frequency_interval_secs(); + }, + frequency::high); + }); + frequency_threads_.at(idx++) = + std::make_unique([this]() -> void { + this->frequency_thread( + [this]() -> std::uint32_t { + return config_->get_low_frequency_interval_secs(); + }, + frequency::low); + }); + frequency_threads_.at(idx++) = + std::make_unique([this]() -> void { + this->frequency_thread([]() -> std::uint32_t { return 1U; }, + frequency::second); + }); } void polling::stop() { mutex_lock lock(start_stop_mutex_); - if (not high_frequency_thread_) { + if (not frequency_threads_.at(0U)) { return; } @@ -120,13 +125,10 @@ void polling::stop() { notify_.notify_all(); thread_lock.unlock(); - high_frequency_thread_->join(); - low_frequency_thread_->join(); - second_frequency_thread_->join(); - - high_frequency_thread_.reset(); - low_frequency_thread_.reset(); - second_frequency_thread_.reset(); + for (auto &&thread : frequency_threads_) { + thread->join(); + thread.reset(); + } event_system::instance().raise("polling"); }