ThreadSchedule 1.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
threadschedule::ScheduledThreadPoolT< PoolType > Class Template Reference

Thread pool augmented with delayed and periodic task scheduling. More...

#include <scheduled_pool.hpp>

Inheritance diagram for threadschedule::ScheduledThreadPoolT< PoolType >:
[legend]

Classes

struct  ScheduledTaskInfo

Public Types

using Task = std::function<void()>
using TimePoint = std::chrono::steady_clock::time_point
using Duration = std::chrono::steady_clock::duration

Public Member Functions

 ScheduledThreadPoolT (size_t worker_threads=std::thread::hardware_concurrency())
 Create a scheduled thread pool.
 ScheduledThreadPoolT (ScheduledThreadPoolT const &)=delete
auto operator= (ScheduledThreadPoolT const &) -> ScheduledThreadPoolT &=delete
auto schedule_after (Duration delay, Task task) -> ScheduledTaskHandle
 Schedule a task to run after a delay.
auto schedule_at (TimePoint time_point, Task task) -> ScheduledTaskHandle
 Schedule a task to run at a specific time point.
auto schedule_periodic (Duration interval, Task task) -> ScheduledTaskHandle
 Schedule a task to run periodically at fixed intervals.
auto schedule_periodic_after (Duration initial_delay, Duration interval, Task task) -> ScheduledTaskHandle
 Schedule a task to run periodically after an initial delay.
auto scheduled_count () const -> size_t
 Get number of scheduled tasks (including periodic)
auto thread_pool () -> PoolType &
 Get the underlying thread pool for direct task submission.
void shutdown ()
 Shutdown the scheduler and wait for completion.
auto configure_threads (std::string const &name_prefix, SchedulingPolicy policy=SchedulingPolicy::OTHER, ThreadPriority priority=ThreadPriority::normal())
 Configure worker threads.

Static Public Member Functions

static void cancel (ScheduledTaskHandle &handle)
 Cancel a scheduled task by handle.

Detailed Description

template<typename PoolType = ThreadPool>
class threadschedule::ScheduledThreadPoolT< PoolType >

Thread pool augmented with delayed and periodic task scheduling.

Non-copyable, non-movable. Combines a dedicated scheduler thread with an underlying PoolType (default: ThreadPool) 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:
  1. Removes it from the multimap.
  2. Checks if the task has been cancelled (via the atomic flag). If cancelled, the task is discarded.
  3. Submits the task to the underlying PoolType via pool_.submit(). From this point on, the task follows the execution rules of the underlying pool (see ThreadPool, FastThreadPool, or HighPerformancePool documentation).
  4. For periodic tasks, the scheduler immediately re-inserts the task into the multimap with next_run += interval. This means the next execution is timed from the scheduled time, not from when the task actually finishes.
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 submitting 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 repeat at a fixed interval, not a fixed rate. If a task takes longer than the interval, executions can pile up because the next run is computed from the previous scheduled time, not from when the task actually finishes.
  • There is no returned std::future for scheduled tasks. If you need to observe the result, use the underlying pool directly via thread_pool().submit().
Thread safety
All schedule_* methods are thread-safe (protected by an internal mutex). cancel() on a ScheduledTaskHandle 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
PoolTypeThread pool used for task execution (default: ThreadPool).
See also
ScheduledThreadPool, ScheduledHighPerformancePool, ScheduledFastThreadPool (convenience aliases)

Definition at line 132 of file scheduled_pool.hpp.

Member Typedef Documentation

◆ Duration

template<typename PoolType = ThreadPool>
using threadschedule::ScheduledThreadPoolT< PoolType >::Duration = std::chrono::steady_clock::duration

Definition at line 137 of file scheduled_pool.hpp.

◆ Task

template<typename PoolType = ThreadPool>
using threadschedule::ScheduledThreadPoolT< PoolType >::Task = std::function<void()>

Definition at line 135 of file scheduled_pool.hpp.

◆ TimePoint

template<typename PoolType = ThreadPool>
using threadschedule::ScheduledThreadPoolT< PoolType >::TimePoint = std::chrono::steady_clock::time_point

Definition at line 136 of file scheduled_pool.hpp.

Constructor & Destructor Documentation

◆ ScheduledThreadPoolT()

template<typename PoolType = ThreadPool>
threadschedule::ScheduledThreadPoolT< PoolType >::ScheduledThreadPoolT ( size_t worker_threads = std::thread::hardware_concurrency())
inlineexplicit

Create a scheduled thread pool.

Parameters
worker_threadsNumber of worker threads for executing tasks (default: hardware concurrency)

Definition at line 153 of file scheduled_pool.hpp.

◆ ~ScheduledThreadPoolT()

template<typename PoolType = ThreadPool>
threadschedule::ScheduledThreadPoolT< PoolType >::~ScheduledThreadPoolT ( )
inline

Definition at line 162 of file scheduled_pool.hpp.

Member Function Documentation

◆ cancel()

template<typename PoolType = ThreadPool>
void threadschedule::ScheduledThreadPoolT< PoolType >::cancel ( ScheduledTaskHandle & handle)
inlinestatic

Cancel a scheduled task by handle.

Parameters
handleHandle returned from schedule_* functions

Note: Can also call handle.cancel() directly

Definition at line 254 of file scheduled_pool.hpp.

◆ configure_threads()

template<typename PoolType = ThreadPool>
auto threadschedule::ScheduledThreadPoolT< PoolType >::configure_threads ( std::string const & name_prefix,
SchedulingPolicy policy = SchedulingPolicy::OTHER,
ThreadPriority priority = ThreadPriority::normal() )
inline

Configure worker threads.

Note: Return type depends on the underlying pool type. ThreadPool returns bool, HighPerformancePool returns expected<void, std::error_code>. For consistent behavior, access the pool directly via thread_pool().

Definition at line 305 of file scheduled_pool.hpp.

◆ schedule_after()

template<typename PoolType = ThreadPool>
auto threadschedule::ScheduledThreadPoolT< PoolType >::schedule_after ( Duration delay,
Task task ) -> ScheduledTaskHandle
inline

Schedule a task to run after a delay.

Parameters
delayDuration to wait before executing the task
taskFunction to execute
Returns
Handle to cancel the task

Definition at line 173 of file scheduled_pool.hpp.

◆ schedule_at()

template<typename PoolType = ThreadPool>
auto threadschedule::ScheduledThreadPoolT< PoolType >::schedule_at ( TimePoint time_point,
Task task ) -> ScheduledTaskHandle
inline

Schedule a task to run at a specific time point.

Parameters
time_pointWhen to execute the task
taskFunction to execute
Returns
Handle to cancel the task

Definition at line 185 of file scheduled_pool.hpp.

Referenced by threadschedule::ScheduledThreadPoolT< ThreadPool >::schedule_after().

◆ schedule_periodic()

template<typename PoolType = ThreadPool>
auto threadschedule::ScheduledThreadPoolT< PoolType >::schedule_periodic ( Duration interval,
Task task ) -> ScheduledTaskHandle
inline

Schedule a task to run periodically at fixed intervals.

Parameters
intervalDuration between executions
taskFunction to execute repeatedly
Returns
Handle to cancel the periodic task

The task runs immediately and then repeats every interval. Use schedule_periodic_after() if you want to delay the first execution.

Definition at line 215 of file scheduled_pool.hpp.

◆ schedule_periodic_after()

template<typename PoolType = ThreadPool>
auto threadschedule::ScheduledThreadPoolT< PoolType >::schedule_periodic_after ( Duration initial_delay,
Duration interval,
Task task ) -> ScheduledTaskHandle
inline

Schedule a task to run periodically after an initial delay.

Parameters
initial_delayDuration to wait before first execution
intervalDuration between subsequent executions
taskFunction to execute repeatedly
Returns
Handle to cancel the periodic task

Definition at line 227 of file scheduled_pool.hpp.

Referenced by threadschedule::ScheduledThreadPoolT< ThreadPool >::schedule_periodic().

◆ scheduled_count()

template<typename PoolType = ThreadPool>
auto threadschedule::ScheduledThreadPoolT< PoolType >::scheduled_count ( ) const -> size_t
inlinenodiscard

Get number of scheduled tasks (including periodic)

Definition at line 262 of file scheduled_pool.hpp.

◆ shutdown()

template<typename PoolType = ThreadPool>
void threadschedule::ScheduledThreadPoolT< PoolType >::shutdown ( )
inline

Shutdown the scheduler and wait for completion.

Definition at line 279 of file scheduled_pool.hpp.

◆ thread_pool()

template<typename PoolType = ThreadPool>
auto threadschedule::ScheduledThreadPoolT< PoolType >::thread_pool ( ) -> PoolType&
inlinenodiscard

Get the underlying thread pool for direct task submission.

Definition at line 271 of file scheduled_pool.hpp.


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