This commit is contained in:
Scott E. Graves 2024-12-03 10:08:19 -06:00
parent d0a8f9df58
commit f480720665
2 changed files with 30 additions and 30 deletions

View File

@ -60,12 +60,10 @@ public:
private: private:
app_config *config_{nullptr}; app_config *config_{nullptr};
std::unique_ptr<std::thread> high_frequency_thread_; std::array<std::unique_ptr<std::thread>, 3U> frequency_threads_;
std::unordered_map<std::string, polling_item> items_; std::unordered_map<std::string, polling_item> items_;
std::unique_ptr<std::thread> low_frequency_thread_;
std::mutex mutex_; std::mutex mutex_;
std::condition_variable notify_; std::condition_variable notify_;
std::unique_ptr<std::thread> second_frequency_thread_;
std::mutex start_stop_mutex_; std::mutex start_stop_mutex_;
stop_type stop_requested_{false}; stop_type stop_requested_{false};

View File

@ -80,28 +80,33 @@ void polling::set_callback(const polling_item &item) {
void polling::start(app_config *config) { void polling::start(app_config *config) {
mutex_lock lock(start_stop_mutex_); mutex_lock lock(start_stop_mutex_);
if (high_frequency_thread_) { if (frequency_threads_.at(0U)) {
return; return;
} }
event_system::instance().raise<service_started>("polling"); event_system::instance().raise<service_started>("polling");
config_ = config; config_ = config;
stop_requested_ = false; stop_requested_ = false;
high_frequency_thread_ = std::make_unique<std::thread>([this]() -> void {
auto idx{0U};
frequency_threads_.at(idx++) =
std::make_unique<std::thread>([this]() -> void {
this->frequency_thread( this->frequency_thread(
[this]() -> std::uint32_t { [this]() -> std::uint32_t {
return config_->get_high_frequency_interval_secs(); return config_->get_high_frequency_interval_secs();
}, },
frequency::high); frequency::high);
}); });
low_frequency_thread_ = std::make_unique<std::thread>([this]() -> void { frequency_threads_.at(idx++) =
std::make_unique<std::thread>([this]() -> void {
this->frequency_thread( this->frequency_thread(
[this]() -> std::uint32_t { [this]() -> std::uint32_t {
return config_->get_low_frequency_interval_secs(); return config_->get_low_frequency_interval_secs();
}, },
frequency::low); frequency::low);
}); });
second_frequency_thread_ = std::make_unique<std::thread>([this]() -> void { frequency_threads_.at(idx++) =
std::make_unique<std::thread>([this]() -> void {
this->frequency_thread([]() -> std::uint32_t { return 1U; }, this->frequency_thread([]() -> std::uint32_t { return 1U; },
frequency::second); frequency::second);
}); });
@ -109,7 +114,7 @@ void polling::start(app_config *config) {
void polling::stop() { void polling::stop() {
mutex_lock lock(start_stop_mutex_); mutex_lock lock(start_stop_mutex_);
if (not high_frequency_thread_) { if (not frequency_threads_.at(0U)) {
return; return;
} }
@ -120,13 +125,10 @@ void polling::stop() {
notify_.notify_all(); notify_.notify_all();
thread_lock.unlock(); thread_lock.unlock();
high_frequency_thread_->join(); for (auto &&thread : frequency_threads_) {
low_frequency_thread_->join(); thread->join();
second_frequency_thread_->join(); thread.reset();
}
high_frequency_thread_.reset();
low_frequency_thread_.reset();
second_frequency_thread_.reset();
event_system::instance().raise<service_shutdown_end>("polling"); event_system::instance().raise<service_shutdown_end>("polling");
} }