Task Scheduler

[中文]

Public header: #include "brookesia/lib_utils/task_scheduler.hpp"

Overview

task_scheduler is a Boost.Asio–based scheduler supporting immediate, delayed, and periodic tasks, with grouped serial/parallel execution.

Features

  • One-shot, delayed, periodic, and batch scheduling

  • Task groups and serial execution control

  • Pause, resume, cancel, and wait for completion

  • Statistics and pre/post callbacks

API Reference

Header File

Classes

class TaskScheduler

Asynchronous task scheduler.

Platform-specific executor, timer, future, and thread types are intentionally hidden behind the implementation object so users of the scheduler do not parse its backend.

Public Functions

TaskScheduler()

Construct an idle task scheduler.

~TaskScheduler()

Stop the scheduler and release all backend resources.

bool start(const StartConfig &config)

Start worker execution with explicit configuration.

bool start()

Start worker execution with the default configuration.

void stop()

Stop workers and cancel pending tasks.

bool is_running() const

Check whether the scheduler is running.

bool is_current_thread_worker() const

Check whether the caller is one of this scheduler's workers.

bool is_current_thread_in_group(const Group &group) const

Check whether the caller is running through a configured task group.

bool try_acquire_worker_wait_slot()

Reserve worker capacity while the current worker blocks for a result.

void release_worker_wait_slot()

Release a worker wait slot held by the current worker.

bool configure_group(const Group &group, const GroupConfig &config)

Configure serialized execution and callbacks for a task group.

bool dispatch(OnceTask task, TaskId *id = nullptr, const Group &group = {})

Dispatch immediately when the current execution context permits it.

bool post(OnceTask task, TaskId *id = nullptr, const Group &group = {})

Post a one-shot task.

bool post_delayed(OnceTask task, int delay_ms, TaskId *id = nullptr, const Group &group = {})

Post a one-shot task after a delay in milliseconds.

bool post_periodic(PeriodicTask task, int interval_ms, TaskId *id = nullptr, const Group &group = {})

Post a periodic task with an interval in milliseconds.

bool post_batch(std::vector<OnceTask> tasks, std::vector<TaskId> *ids = nullptr, const Group &group = {})

Post multiple one-shot tasks to the same group.

void cancel(TaskId id)

Cancel a task.

void cancel_group(const Group &group)

Cancel every task in a group.

void cancel_all()

Cancel all tracked tasks.

bool suspend(TaskId id)

Suspend a delayed or periodic task.

size_t suspend_group(const Group &group)

Suspend all suspendable tasks in a group.

size_t suspend_all()

Suspend all suspendable tasks.

bool resume(TaskId id)

Resume a suspended task.

size_t resume_group(const Group &group)

Resume all suspended tasks in a group.

size_t resume_all()

Resume all suspended tasks.

bool wait(TaskId id, int timeout_ms = -1)

Wait for one task, or indefinitely when timeout is negative.

bool wait_group(const Group &group, int timeout_ms = -1)

Wait for every task in a group.

bool wait_all(int timeout_ms = -1)

Wait for every tracked task.

bool restart_timer(TaskId id)

Restart the countdown of a running delayed or periodic task.

TaskType get_type(TaskId id) const

Get the type of a task.

TaskState get_state(TaskId id) const

Get the state of a task.

Group get_group(TaskId id) const

Get the group assigned to a task.

size_t get_group_task_count(const Group &group) const

Get the number of tracked tasks in a group.

std::vector<Group> get_active_groups() const

Get all groups that currently contain tasks.

Statistics get_statistics() const

Get accumulated task statistics.

size_t get_worker_count() const

Get the number of active worker threads.

void reset_statistics()

Reset accumulated task statistics.