Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

Commit c7f1257

Browse files
author
Luc Sinet
committed
develop #comment Add periodic tasks support to the scheduler.
1 parent df41afb commit c7f1257

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

include/Scheduler.hh

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <future>
77
#include <mutex>
88
#include <string>
9+
#include <unordered_map>
910
#include <utility>
1011

1112
#include "detail/PriorityQueue.hpp"
@@ -62,21 +63,46 @@ class Scheduler {
6263
this->processTasks();
6364
}
6465
};
66+
std::lock_guard<std::mutex> guard(_mutex);
6567
this->addTask(id, std::move(functor), timepoint);
6668

6769
return future;
6870
}
6971

72+
template <typename Duration, class F, class... Args>
73+
void scheduleEvery(const std::string& id, Duration delay, F&& function, Args&&... args) {
74+
auto task = std::bind(std::forward<F>(function), std::forward<Args>(args)...);
75+
76+
auto periodicTask = [this, id, task = std::move(task), delay]() {
77+
task();
78+
79+
{
80+
std::lock_guard<std::mutex> guard(_mutex);
81+
82+
--_workerCount;
83+
auto periodicTask = _periodicTasks.find(id);
84+
if (periodicTask == _periodicTasks.end()) {
85+
return;
86+
}
87+
this->addTask(id, periodicTask->second, Detail::Clock::now() + delay);
88+
}
89+
};
90+
std::lock_guard<std::mutex> guard(_mutex);
91+
this->addTask(id, std::move(periodicTask), Detail::Clock::now() + delay, true);
92+
}
93+
7094
void remove(const std::string& id);
7195
bool isScheduled(const std::string& id) const;
7296

7397
private:
74-
void addTask(const std::string& id, std::function<void()> functor, Detail::Timepoint timepoint);
98+
void addTask(const std::string& id, std::function<void()> functor, Detail::Timepoint timepoint,
99+
bool reschedulable = false);
75100
void processTasks();
76101

77102
private:
78103
std::shared_ptr<Detail::Threadpool> _threadpool;
79104
Detail::PriorityQueue<TimedTask, std::greater<>> _tasks;
105+
std::unordered_map<std::string, std::function<void()>> _periodicTasks;
80106
std::hash<std::string> _hasher;
81107
mutable std::mutex _mutex;
82108
size_t _maxWorkers;

src/Scheduler.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ std::future<void> Scheduler::stop(bool discard) {
1212
std::lock_guard<std::mutex> guard(_mutex);
1313

1414
_stopped = true;
15+
_periodicTasks.clear();
1516
if (discard) {
1617
_tasks.clear();
1718
}
@@ -57,12 +58,14 @@ bool Scheduler::isScheduled(const std::string& id) const {
5758

5859
// Private methods
5960

60-
void Scheduler::addTask(const std::string& id, std::function<void()> functor, Detail::Timepoint timepoint) {
61-
std::lock_guard<std::mutex> guard(_mutex);
62-
61+
void Scheduler::addTask(const std::string& id, std::function<void()> functor, Detail::Timepoint timepoint,
62+
bool reschedulable) {
6363
if (_stopped) {
6464
return;
6565
}
66+
if (reschedulable) {
67+
_periodicTasks[id] = functor;
68+
}
6669
_tasks.emplace(_hasher(id), std::move(functor), timepoint);
6770
this->processTasks();
6871
}

0 commit comments

Comments
 (0)