Dripline-Cpp  v2.4.2
Dripline Implementation in C++
Classes | Public Types | Public Member Functions | Public Attributes | List of all members
scheduler< executor, clock > Class Template Reference

Executes scheduled events. More...

#include <scheduler.hh>

Inheritance diagram for scheduler< executor, clock >:
Inheritance graph

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, eventevents_map_t
 

Public Member Functions

 scheduler ()
 
 scheduler (const scheduler &)=delete
 
 scheduler (scheduler &&)
 
virtual ~scheduler ()
 
scheduleroperator= (const scheduler &)=delete
 
scheduleroperator= (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
 

Detailed Description

template<typename executor = simple_executor, typename clock = std::chrono::system_clock>
class dripline::scheduler< executor, clock >

Executes scheduled events.

Author
N.S. Oblath

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.

Member Typedef Documentation

◆ clock_t

using clock_t = clock

Definition at line 96 of file scheduler.hh.

◆ duration_t

using duration_t = typename clock::duration

Definition at line 98 of file scheduler.hh.

◆ events_map_t

typedef std::multimap< time_point_t, event > events_map_t

Definition at line 110 of file scheduler.hh.

◆ executable_t

using executable_t = std::function< void() >

Definition at line 99 of file scheduler.hh.

◆ time_point_t

using time_point_t = typename clock::time_point

Definition at line 97 of file scheduler.hh.

Constructor & Destructor Documentation

◆ scheduler() [1/3]

scheduler ( )

Definition at line 173 of file scheduler.hh.

◆ scheduler() [2/3]

scheduler ( const scheduler< executor, clock > &  )
delete

◆ scheduler() [3/3]

scheduler ( scheduler< executor, clock > &&  )

◆ ~scheduler()

~scheduler ( )
virtual

Definition at line 197 of file scheduler.hh.

Member Function Documentation

◆ execute()

void execute ( )

Main execution loop for the scheduler.

Definition at line 321 of file scheduler.hh.

◆ operator=() [1/2]

scheduler& operator= ( const scheduler< executor, clock > &  )
delete

◆ operator=() [2/2]

scheduler& operator= ( scheduler< executor, clock > &&  )

◆ schedule() [1/2]

int schedule ( executable_t  an_executable,
time_point_t  an_exe_time 
)

Schedule a one-off event

Parameters
an_executableThe executable to be used for the event
an_exe_timeThe time at which to execute the event
Returns
The ID for the scheduled event.

Definition at line 215 of file scheduler.hh.

◆ schedule() [2/2]

int schedule ( executable_t  an_executable,
duration_t  an_interval,
time_point_t  an_exe_time = clock::now() 
)

Schedule a repeating event

Parameters
an_executableThe executable to be used for the event
an_intervalThe repetition interval
an_exe_timeThe first execution time; default: now.
Returns
The ID for the scheduled event.

Definition at line 240 of file scheduler.hh.

◆ snake_case_mv_accessible() [1/2]

snake_case_mv_accessible ( duration_t  ,
exe_buffer   
)

The time difference from "now" that determines whether an event is executed.

◆ snake_case_mv_accessible() [2/2]

snake_case_mv_accessible ( duration_t  ,
cycle_time   
)

Main thread cycle time.

◆ snake_case_mv_referrable_const() [1/2]

snake_case_mv_referrable_const ( executor  ,
the_executor   
)

The executor used to execute events.

◆ snake_case_mv_referrable_const() [2/2]

snake_case_mv_referrable_const ( events_map_t  ,
events   
)

The scheduled events, stored in a map sorted by execution time.

◆ unschedule()

void unschedule ( int  an_id)

Unschedule an event using the event's ID.

Definition at line 298 of file scheduler.hh.

Member Data Documentation

◆ f_cv

std::condition_variable_any f_cv

Definition at line 165 of file scheduler.hh.

◆ f_executor_mutex

std::mutex f_executor_mutex

Definition at line 163 of file scheduler.hh.

◆ f_scheduler_mutex

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.

◆ f_scheduler_thread

std::thread f_scheduler_thread

Definition at line 166 of file scheduler.hh.


The documentation for this class was generated from the following file: