Flexiv RDK APIs  1.5.1
Public Member Functions | List of all members
flexiv::rdk::Scheduler Class Reference

Real-time scheduler that can simultaneously run multiple periodic tasks. Parameters for each task are configured independently. More...

#include <scheduler.hpp>

Public Member Functions

 Scheduler ()
 [Blocking] Create an instance and initialize the real-time scheduler. More...
 
void AddTask (std::function< void(void)> &&callback, const std::string &task_name, int interval, int priority, int cpu_affinity=-1)
 [Non-blocking] Add a new periodic task to the scheduler's task pool. Each task in the pool is assigned to a dedicated thread with independent thread configuration. More...
 
void Start ()
 [Blocking] Start all added tasks. A dedicated thread will be created for each added task and the periodic execution will begin. More...
 
void Stop ()
 [Blocking] Stop all added tasks. The periodic execution will stop and all task threads will be closed with the resources released. More...
 
int max_priority () const
 [Non-blocking] Get maximum available priority for user tasks. More...
 
int min_priority () const
 [Non-blocking] Get minimum available priority for user tasks. More...
 
size_t num_tasks () const
 [Non-blocking] Get number of tasks added to the scheduler. More...
 

Detailed Description

Real-time scheduler that can simultaneously run multiple periodic tasks. Parameters for each task are configured independently.

Definition at line 21 of file scheduler.hpp.

Constructor & Destructor Documentation

◆ Scheduler()

flexiv::rdk::Scheduler::Scheduler ( )

[Blocking] Create an instance and initialize the real-time scheduler.

Exceptions
std::runtime_errorif the initialization sequence failed.
Warning
This constructor blocks until the initialization sequence is successfully finished.

Member Function Documentation

◆ AddTask()

void flexiv::rdk::Scheduler::AddTask ( std::function< void(void)> &&  callback,
const std::string &  task_name,
int  interval,
int  priority,
int  cpu_affinity = -1 
)

[Non-blocking] Add a new periodic task to the scheduler's task pool. Each task in the pool is assigned to a dedicated thread with independent thread configuration.

Parameters
[in]callbackCallback function of user task.
[in]task_nameA unique name for this task.
[in]intervalExecution interval of this periodic task [ms]. The minimum available interval is 1 ms, equivalent to 1 kHz loop frequency.
[in]priorityPriority for this task thread, can be set to min_priority()–max_priority() for real-time scheduling, or 0 for non-real-time scheduling. When the priority is set to use real-time scheduling, this thread becomes a real-time thread and can only be interrupted by threads with higher priority. When the priority is set to use non-real-time scheduling (i.e. 0), this thread becomes a non-real-time thread and can be interrupted by any real-time threads. The common practice is to set priority of the most critical tasks to max_priority() or near, and set priority of other non-critical tasks to 0 or near. To avoid race conditions, the same priority should be assigned to only one task.
[in]cpu_affinityCPU core for this task thread to bind to, can be set to 2–(num_cores
  • 1). This task thread will only run on the specified CPU core. If left with the default value (-1), then this task thread will not bind to any CPU core, and the system will decide which core to run this task thread on according to the system's own strategy. The common practice is to bind the high-priority task to a dedicated spare core, and bind low-priority tasks to other cores or just leave them unbound (cpu_affinity = -1).
Exceptions
std::logic_errorif the scheduler is already started or is not fully initialized yet.
std::invalid_argumentif the specified interval/priority/affinity is invalid or the specified task name is duplicate.
std::runtime_errorif an error is triggered by the client computer.
Note
Setting CPU affinity on macOS has no effect, as its Mach kernel takes full control of thread placement so CPU binding is not supported.
Warning
Calling this function after start() is not allowed.
For maximum scheduling performance, setting CPU affinity to 0 or 1 is not allowed: core 0 is usually the default core for system processes and can be crowded; core 1 is reserved for the scheduler itself.

◆ max_priority()

int flexiv::rdk::Scheduler::max_priority ( ) const

[Non-blocking] Get maximum available priority for user tasks.

Returns
The maximum priority that can be set for a user task with real-time scheduling policy when calling AddTask().

◆ min_priority()

int flexiv::rdk::Scheduler::min_priority ( ) const

[Non-blocking] Get minimum available priority for user tasks.

Returns
The minimum priority that can be set for a user task with real-time scheduling policy when calling AddTask().

◆ num_tasks()

size_t flexiv::rdk::Scheduler::num_tasks ( ) const

[Non-blocking] Get number of tasks added to the scheduler.

Returns
Number of added tasks.

◆ Start()

void flexiv::rdk::Scheduler::Start ( )

[Blocking] Start all added tasks. A dedicated thread will be created for each added task and the periodic execution will begin.

Exceptions
std::logic_errorif the scheduler is not initialized yet.
std::runtime_errorif failed to start the tasks.
Note
This function blocks until all added tasks are started.

◆ Stop()

void flexiv::rdk::Scheduler::Stop ( )

[Blocking] Stop all added tasks. The periodic execution will stop and all task threads will be closed with the resources released.

Exceptions
std::logic_errorif the scheduler is not initialized or the tasks are not started yet.
std::runtime_errorif failed to stop the tasks.
Note
Calling start() again can restart the added tasks.
This function blocks until all task threads have exited and resources are released.
Warning
This function cannot be called from within a task thread.

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