Refactored polling to be more accurate on scheduling tasks

This commit is contained in:
Scott E. Graves 2025-02-14 08:35:12 -06:00
parent fc573e165b
commit 1b4c1db44d
2 changed files with 41 additions and 30 deletions

View File

@ -13,6 +13,7 @@
### Changes from v2.0.3-rc ### Changes from v2.0.3-rc
* Continue documentation updates * Continue documentation updates
* Refactored polling to be more accurate on scheduling tasks
## v2.0.3-rc ## v2.0.3-rc

View File

@ -38,7 +38,13 @@ void polling::frequency_thread(
std::function<std::uint32_t()> get_frequency_seconds, frequency freq) { std::function<std::uint32_t()> get_frequency_seconds, frequency freq) {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
auto last_run = std::chrono::system_clock::time_point::min();
while (not get_stop_requested()) { while (not get_stop_requested()) {
auto elapsed = std::chrono::system_clock::now() - last_run;
auto max_elapsed = std::chrono::seconds(get_frequency_seconds());
if (std::chrono::duration_cast<std::chrono::seconds>(elapsed) >=
max_elapsed) {
unique_mutex_lock lock(mutex_); unique_mutex_lock lock(mutex_);
auto futures = std::accumulate( auto futures = std::accumulate(
items_.begin(), items_.end(), std::deque<tasks::task_ptr>{}, items_.begin(), items_.end(), std::deque<tasks::task_ptr>{},
@ -73,12 +79,16 @@ void polling::frequency_thread(
futures.pop_front(); futures.pop_front();
} }
last_run = std::chrono::system_clock::now();
elapsed = std::chrono::seconds(0U);
}
unique_mutex_lock lock(mutex_);
if (get_stop_requested()) { if (get_stop_requested()) {
return; return;
} }
lock.lock(); notify_.wait_for(lock, max_elapsed - elapsed);
notify_.wait_for(lock, std::chrono::seconds(get_frequency_seconds()));
} }
} }