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

Thread pool with support for scheduled and periodic tasks. 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 with support for scheduled and periodic tasks.

Features:

  • Schedule tasks to run at specific time points
  • Schedule tasks to run after a delay
  • Schedule periodic tasks with fixed intervals
  • Cancel scheduled tasks before they execute
  • Integrates with any thread pool type (ThreadPool by default)
Template Parameters
PoolTypeType of thread pool to use for task execution (default: ThreadPool)

Definition at line 66 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 71 of file scheduled_pool.hpp.

◆ Task

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

Definition at line 69 of file scheduled_pool.hpp.

◆ TimePoint

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

Definition at line 70 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 87 of file scheduled_pool.hpp.

◆ ~ScheduledThreadPoolT()

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

Definition at line 96 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 188 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 239 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 107 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 119 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 149 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 161 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 196 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 213 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 205 of file scheduled_pool.hpp.


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