![]() |
Dripline-Cpp
v2.4.2
Dripline Implementation in C++
|
Executes scheduled events. More...
#include <scheduler.hh>
Classes | |
struct | event |
Definition of an event, including the executable object and the scheduler ID. More... | |
Public Types | |
using | clock_t = clock |
using | time_point_t = typename clock::time_point |
using | duration_t = typename clock::duration |
using | executable_t = std::function< void() > |
typedef std::multimap< time_point_t, event > | events_map_t |
Public Member Functions | |
scheduler () | |
scheduler (const scheduler &)=delete | |
scheduler (scheduler &&) | |
virtual | ~scheduler () |
scheduler & | operator= (const scheduler &)=delete |
scheduler & | operator= (scheduler &&) |
int | schedule (executable_t an_executable, time_point_t an_exe_time) |
int | schedule (executable_t an_executable, duration_t an_interval, time_point_t an_exe_time=clock::now()) |
void | unschedule (int an_id) |
Unschedule an event using the event's ID. More... | |
void | execute () |
Main execution loop for the scheduler. More... | |
snake_case_mv_accessible (duration_t, exe_buffer) | |
The time difference from "now" that determines whether an event is executed. More... | |
snake_case_mv_accessible (duration_t, cycle_time) | |
Main thread cycle time. More... | |
snake_case_mv_referrable_const (executor, the_executor) | |
The executor used to execute events. More... | |
snake_case_mv_referrable_const (events_map_t, events) | |
The scheduled events, stored in a map sorted by execution time. More... | |
Public Attributes | |
snake_case_mv_accessible_static(int, curr_id) protected std::recursive_mutex | f_scheduler_mutex |
The ID to be used for the next scheduled event. More... | |
std::mutex | f_executor_mutex |
std::condition_variable_any | f_cv |
std::thread | f_scheduler_thread |
Executes scheduled events.
An event is an executable object (e.g. a std::function object, or a lambda) with the signature void ()
.
Events can be one-off, scheduled for a particular time, or they can be repeating, scheduled with an interval starting at a particular time. The default start time for repeating events is "now."
This class is thread safe. Execution is meant to occur in one thread, with events being scheduled from different threads.
In general an event will be executed when it is within one "execution buffer" of now (where "now" is the time at which the scheduler is considering the next event). The default execution buffer time is 50 ms.
There are a variety of factors that can make execution of an event late. For instance, if the execution time of events is long compared to the time between them, then events will start to get delayed. This could potentially be allayed by implementing a more complex executor class that uses a pool of threaded workers, but that is not supplied in the current implementation.
Each scheduled thread is assigned a unique ID, which is returned when the event is scheduled. That ID can be used to unschedule an event (repeating or one-off).
Definition at line 93 of file scheduler.hh.
using clock_t = clock |
Definition at line 96 of file scheduler.hh.
using duration_t = typename clock::duration |
Definition at line 98 of file scheduler.hh.
typedef std::multimap< time_point_t, event > events_map_t |
Definition at line 110 of file scheduler.hh.
using executable_t = std::function< void() > |
Definition at line 99 of file scheduler.hh.
using time_point_t = typename clock::time_point |
Definition at line 97 of file scheduler.hh.
scheduler | ( | ) |
Definition at line 173 of file scheduler.hh.
|
virtual |
Definition at line 197 of file scheduler.hh.
void execute | ( | ) |
Main execution loop for the scheduler.
Definition at line 321 of file scheduler.hh.
int schedule | ( | executable_t | an_executable, |
time_point_t | an_exe_time | ||
) |
Schedule a one-off event
an_executable | The executable to be used for the event |
an_exe_time | The time at which to execute the event |
Definition at line 215 of file scheduler.hh.
int schedule | ( | executable_t | an_executable, |
duration_t | an_interval, | ||
time_point_t | an_exe_time = clock::now() |
||
) |
Schedule a repeating event
an_executable | The executable to be used for the event |
an_interval | The repetition interval |
an_exe_time | The first execution time; default: now. |
Definition at line 240 of file scheduler.hh.
snake_case_mv_accessible | ( | duration_t | , |
exe_buffer | |||
) |
The time difference from "now" that determines whether an event is executed.
snake_case_mv_accessible | ( | duration_t | , |
cycle_time | |||
) |
Main thread cycle time.
snake_case_mv_referrable_const | ( | executor | , |
the_executor | |||
) |
The executor used to execute events.
snake_case_mv_referrable_const | ( | events_map_t | , |
events | |||
) |
The scheduled events, stored in a map sorted by execution time.
void unschedule | ( | int | an_id | ) |
Unschedule an event using the event's ID.
Definition at line 298 of file scheduler.hh.
std::condition_variable_any f_cv |
Definition at line 165 of file scheduler.hh.
std::mutex f_executor_mutex |
Definition at line 163 of file scheduler.hh.
snake_case_mv_accessible_static ( int, curr_id ) protected std::recursive_mutex f_scheduler_mutex |
The ID to be used for the next scheduled event.
Definition at line 156 of file scheduler.hh.
std::thread f_scheduler_thread |
Definition at line 166 of file scheduler.hh.