task_scheduler

class task_scheduler

Task Scheduler that can launch tasks on based several time-based policies.

This class is used to manage a queue of tasks using a fixed number of threads. The actual task execution is delgated to an internal ssts::task_pool object.

Public Functions

inline explicit task_scheduler(const unsigned int num_threads = std::thread::hardware_concurrency())

Constructor.

Creates a ssts::task_scheduler instance. The number of threads to be used by the ssts::task_pool defaults to the number of threads supported by the platform.

Parameters

num_threads – Number of threads that will be used in the underlying ssts::task_pool.

inline ~task_scheduler()

Destructor.

Destructs this. If the task_scheduler is running its tasks are stopped first.

inline void start()

Start running tasks.

This function starts the task_scheduler worker thread. The function is guaranteed to return after the scheduler thread is started.

inline size_t size()

Get the number of scheduled tasks.

This function return the number of tasks that are currently scheduled for execution (both enabled and disabled).

Returns

Number of tasks to be run.

inline void stop()

Stop all running tasks.

This function stops the task_scheduler execution and stops all the running tasks.

inline bool is_duplicate_allowed() const

Check if duplicated tasks are allowed.

Duplicated tasks are created with the same task_id. If a task has been started without a task_id it is not possible to check if it has duplicates. In case duplicates are not allowed task insertion will be silently rejected for same task_id.

Returns

bool indicating if duplicated tasks are allowed.

inline void set_duplicate_allowed(bool is_allowed)

Enable or disable duplicated tasks.

Duplicated tasks are created with the same task_id. If a task has been started without a task_id it is not possible to check if it has duplicates. In case duplicates are not allowed task insertion will be silently rejected for same task_id.

inline bool is_scheduled(const std::string &task_id)

Check if a task is scheduled.

If a task has been started without a task_id it is not possible to query its status. In case a task_id is not found this function return false. If a task is no longer scheduled it must be added using one of the following APIs: ssts::task_scheduler::in, ssts::task_scheduler::at, ssts::task_scheduler::every.

Parameters

task_id – task_id to check.

Returns

bool indicating if the task is currently scheduled.

inline bool is_enabled(const std::string &task_id)

Check if a task is enabled.

If a task has been started without a task_id it is not possible to query its status. In case a task_id is not found this function return false. By default new tasks are enabled. A task can be enabled or disabled by calling ssts::task_scheduler::set_enabled.

Parameters

task_id – task_id to check.

Returns

bool indicating if the task is currently enabled.

inline bool set_enabled(const std::string &task_id, bool is_enabled)

Enable or disable task.

If a task has been started without a task_id it is not possible to update its status. In case a task_id is not found this function return false. It is possible to check if a task is enabled or disabled by calling ssts::task_scheduler::is_enabled.

Parameters
  • task_id – task_id to enable or disable.

  • is_enabled – true enables, false disables the given task_id.

Returns

bool indicating if the task is currently enabled.

inline bool remove_task(const std::string &task_id)

Remove a task.

If a task has been started without a task_id it is not possible to remove it. In case a task_id is not found this function return false. It is possible to check if a task is scheduled by calling ssts::task_scheduler::is_scheduled.

Parameters

task_id – task_id to remove.

Returns

bool indicating if the task has been properly removed.

inline bool update_interval(const std::string &task_id, ssts::clock::duration interval)

Update a task interval.

If a task is not recursive (i.e. has not been started with every() APIs) or the task has not been assigned a task_id, it is not possible to update it. In case of any failure (task_id not found or task non recursive) this function return false.

Parameters
  • task_id – task_id to update.

  • interval – new task interval to set.

Returns

bool indicating if the task has been properly updated.