|
| | scheduled_pool_backend_base (size_t worker_threads=default_worker_count()) |
| | Create a scheduled thread pool.
|
| template<typename T = PoolType, std::enable_if_t< std::is_constructible_v< T, size_t, bool >, int > = 0> |
| | scheduled_pool_backend_base (size_t worker_threads, bool register_workers) |
| | scheduled_pool_backend_base (scheduled_pool_backend_base const &)=delete |
| auto | operator= (scheduled_pool_backend_base const &) -> scheduled_pool_backend_base &=delete |
| | ~scheduled_pool_backend_base () |
| auto | schedule_after (duration delay, task_type task) -> scheduled_task_backend |
| | Schedule a task to run after a delay.
|
| template<typename F, std::enable_if_t<!std::is_same_v< detail::remove_cvref_t< F >, task_type >, int > = 0> |
| auto | schedule_after (duration delay, F &&task) -> scheduled_task_backend |
| auto | schedule_at (time_point time_point, task_type task) -> scheduled_task_backend |
| | Schedule a task to run at a specific time point.
|
| template<typename F, std::enable_if_t<!std::is_same_v< detail::remove_cvref_t< F >, task_type >, int > = 0> |
| auto | schedule_at (time_point time_point, F &&task) -> scheduled_task_backend |
| auto | schedule_periodic (duration interval, task_type task) -> scheduled_task_backend |
| | Schedule a task to run periodically at fixed intervals.
|
| template<typename F, std::enable_if_t<!std::is_same_v< detail::remove_cvref_t< F >, task_type >, int > = 0> |
| auto | schedule_periodic (duration interval, F &&task) -> scheduled_task_backend |
| auto | schedule_periodic_after (duration initial_delay, duration interval, task_type task) -> scheduled_task_backend |
| | Schedule a task to run periodically after an initial delay.
|
| template<typename F, std::enable_if_t<!std::is_same_v< detail::remove_cvref_t< F >, task_type >, int > = 0> |
| auto | schedule_periodic_after (duration initial_delay, duration interval, F &&task) -> scheduled_task_backend |
| auto | scheduled_count () const -> size_t |
| | Get number of scheduled tasks (including periodic).
|
| auto | is_current_context () const noexcept -> bool |
| auto | thread_pool () -> PoolType & |
| | Get the underlying thread pool for direct task submission.
|
| void | shutdown (shutdown_policy_backend policy=shutdown_policy_backend::drain) |
| | Shutdown the scheduler and wait for completion.
|
| auto | configure_threads (std::string const &name_prefix, native_scheduling_policy policy=native_scheduling_policy::other, native_thread_priority priority=native_thread_priority::normal()) |
| | Configure worker threads.
|
| auto | configure_threads (native_thread_config const &config) -> expected< void, std::error_code > |
| auto | scheduler_thread_info () const -> std::optional< thread_info > |
| auto | configure_scheduler_thread (std::string const &name, native_scheduling_policy policy=native_scheduling_policy::other, native_thread_priority priority=native_thread_priority::normal()) -> expected< void, std::error_code > |
| auto | configure_scheduler_thread (native_thread_config const &config) -> expected< void, std::error_code > |
template<typename PoolType = thread_pool_backend>
class threadschedule::detail::scheduled_pool_backend_base< PoolType >
Thread pool augmented with delayed and periodic task scheduling.
Non-copyable, non-movable. Combines a dedicated scheduler thread with an underlying PoolType (default: thread_pool_backend) that does the actual work.
- How task execution works
- The pool owns a single scheduler thread that runs an internal loop (scheduler_loop). Scheduled tasks are stored in a std::multimap sorted by their next_run time point. The scheduler thread sleeps (via condition_variable::wait / wait_until) until the earliest task is due. When a task becomes due, the scheduler thread:
- Removes it from the multimap.
- Checks if the task has been cancelled (via the atomic flag). If cancelled, the task is discarded.
- Posts the task to the underlying PoolType via pool_.post(). From this point on, the task follows the execution rules of the underlying pool (see
thread_pool_backend, polling_pool_backend, work_stealing_pool_backend, or lightweight_pool_backend documentation).
- For periodic tasks, the scheduler re-inserts the next fixed-rate deadline. A task never overlaps with itself; deadlines missed while an occurrence is running are skipped rather than queued as backlog.
- Execution guarantees
- Every successfully scheduled task (schedule_after/schedule_at/ schedule_periodic returned a handle) is guaranteed to eventually execute, unless it is cancelled or shutdown() is called before it becomes due.
- Tasks are stored in a std::multimap keyed by time point. When multiple tasks share the same due time, they are dispatched in insertion order (guaranteed by std::multimap since C++11).
- Tasks that are already due and submitted to the underlying pool before shutdown() will still execute (the pool drains its queue).
- Tasks that are not yet due at the time of shutdown() will NOT execute. The scheduler thread exits immediately on shutdown, so future-scheduled tasks are lost.
- Cancellation is cooperative: calling handle.cancel() sets an atomic flag. The scheduler checks this flag before posting the task to the pool. Additionally, the pool-side wrapper checks the flag again right before calling the task. However, a task that is already running will NOT be interrupted by cancel().
- Periodic tasks use fixed-rate deadlines. If an occurrence takes longer than its interval, missed occurrences are skipped. The same periodic callable is never invoked concurrently, and independent pool work does not wait behind a backlog of overdue occurrences.
- There is no returned std::future for scheduled tasks. If you need to observe the result, use the underlying pool directly via thread_pool().post() or thread_pool().submit().
- Thread safety
- All schedule_* methods are thread-safe (protected by an internal mutex). cancel() on a scheduled_task_backend is also thread-safe (atomic). shutdown() is internally guarded and safe to call more than once.
- Lifetime
- The destructor calls shutdown(), which joins the scheduler thread and then shuts down the underlying pool. Can block if the pool still has running tasks.
- Copyability / movability
- Not copyable, not movable.
- Template Parameters
-
- See also
- scheduled_pool_backend, scheduled_work_stealing_pool_backend, scheduled_polling_pool_backend, scheduled_lightweight_pool_backend (convenience aliases)
Definition at line 104 of file scheduled_pool_backend_base.hpp.