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:
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::unique_ptr<std::thread> low_frequency_thread_;
std::mutex mutex_;
std::condition_variable notify_;
std::unique_ptr<std::thread> second_frequency_thread_;
std::mutex start_stop_mutex_;
stop_type stop_requested_{false};

View File

@ -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<service_started>("polling");
config_ = config;
stop_requested_ = false;
high_frequency_thread_ = std::make_unique<std::thread>([this]() -> void {
this->frequency_thread(
[this]() -> std::uint32_t {
return config_->get_high_frequency_interval_secs();
},
frequency::high);
});
low_frequency_thread_ = std::make_unique<std::thread>([this]() -> void {
this->frequency_thread(
[this]() -> std::uint32_t {
return config_->get_low_frequency_interval_secs();
},
frequency::low);
});
second_frequency_thread_ = std::make_unique<std::thread>([this]() -> void {
this->frequency_thread([]() -> std::uint32_t { return 1U; },
frequency::second);
});
auto idx{0U};
frequency_threads_.at(idx++) =
std::make_unique<std::thread>([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<std::thread>([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<std::thread>([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<service_shutdown_end>("polling");
}