|
ThreadSchedule 2.2.0
Modern C++ thread management library
|
High-performance thread pool optimized for high-frequency task submission. More...
#include <thread_pool.hpp>
Classes | |
| struct | Statistics |
Public Types | |
| using | Task = std::function<void()> |
| using | QueuedTask = detail::move_callable<void()> |
Public Member Functions | |
| HighPerformancePool (size_t num_threads=std::thread::hardware_concurrency(), size_t deque_capacity=WorkStealingDeque< QueuedTask >::DEFAULT_CAPACITY, bool register_workers=false) | |
| HighPerformancePool (HighPerformancePool const &)=delete | |
| auto | operator= (HighPerformancePool const &) -> HighPerformancePool &=delete |
| ~HighPerformancePool () | |
| void | shutdown (ShutdownPolicy policy=ShutdownPolicy::drain) |
| Shut the pool down. | |
| auto | shutdown_for (std::chrono::milliseconds timeout) -> bool |
Attempt a timed drain: finish as many tasks as possible within timeout, then force-stop remaining workers. | |
| template<typename F, typename... Args> | |
| auto | try_submit (F &&f, Args &&... args) -> expected< std::future< std::invoke_result_t< F, Args... > >, std::error_code > |
| Submit a task without throwing on shutdown. | |
| template<typename F, typename... Args> | |
| auto | submit (F &&f, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > > |
| Submit a task, throwing on shutdown. | |
| template<typename F, typename... Args> | |
| void | post (F &&f, Args &&... args) |
| Fire-and-forget task submission (throwing variant). | |
| template<typename F, typename... Args> | |
| auto | try_post (F &&f, Args &&... args) -> expected< void, std::error_code > |
| Fire-and-forget task submission (non-throwing variant). | |
| template<typename Iterator> | |
| auto | try_submit_batch (Iterator begin, Iterator end) -> expected< std::vector< std::future< void > >, std::error_code > |
Submit a range of void() callables in one go (non-throwing). | |
| template<typename Iterator> | |
| auto | submit_batch (Iterator begin, Iterator end) -> std::vector< std::future< void > > |
Submit a range of void() callables in one go (throwing). | |
| template<typename Iterator, typename F> | |
| void | parallel_for_each (Iterator begin, Iterator end, F &&func) |
Apply func to every element in [begin, end) in parallel. | |
Observers | |
| auto | size () const noexcept -> size_t |
| Number of worker threads in this pool. | |
| auto | pending_tasks () const -> size_t |
| Approximate count of tasks waiting in all queues. | |
| auto | get_statistics () const -> Statistics |
| Collect approximate performance counters. | |
Thread configuration | |
| auto | configure_threads (std::string const &name_prefix, SchedulingPolicy policy=SchedulingPolicy::OTHER, ThreadPriority priority=ThreadPriority::normal()) -> expected< void, std::error_code > |
| Name, schedule and prioritize all worker threads. | |
| auto | set_affinity (ThreadAffinity const &affinity) -> expected< void, std::error_code > |
| Pin all workers to the same CPU set. | |
| auto | distribute_across_cpus () -> expected< void, std::error_code > |
| Pin each worker to a distinct CPU core (round-robin). | |
Synchronisation | |
| void | wait_for_tasks () |
| Block until all pending and active tasks have completed. | |
Tracing hooks | |
| void | set_on_task_start (TaskStartCallback cb) |
| Register a callback invoked just before each task executes. | |
| template<typename Callback, std::enable_if_t<!std::is_same_v< detail::remove_cvref_t< Callback >, TaskStartCallback >, int > = 0> | |
| void | set_on_task_start (Callback &&cb) |
| void | set_on_task_end (TaskEndCallback cb) |
| Register a callback invoked just after each task completes. | |
| template<typename Callback, std::enable_if_t<!std::is_same_v< detail::remove_cvref_t< Callback >, TaskEndCallback >, int > = 0> | |
| void | set_on_task_end (Callback &&cb) |
High-performance thread pool optimized for high-frequency task submission.
Uses a work-stealing architecture: each worker thread owns a private WorkStealingDeque, and idle workers attempt to steal tasks from other workers' queues. A shared overflow queue absorbs bursts when all per-thread queues are full.
Optimizations for 1k+ tasks with 10k+ tasks/second throughput:
Definition at line 562 of file thread_pool.hpp.
| using threadschedule::HighPerformancePool::QueuedTask = detail::move_callable<void()> |
Definition at line 566 of file thread_pool.hpp.
| using threadschedule::HighPerformancePool::Task = std::function<void()> |
Definition at line 565 of file thread_pool.hpp.
|
inlineexplicit |
Definition at line 579 of file thread_pool.hpp.
References threadschedule::WorkStealingDeque< T >::DEFAULT_CAPACITY.
Referenced by HighPerformancePool(), and operator=().
|
delete |
References HighPerformancePool().
|
inline |
Definition at line 602 of file thread_pool.hpp.
References threadschedule::drain, and shutdown().
|
inline |
Name, schedule and prioritize all worker threads.
Each worker is named name_prefix + "_0", "_1", etc.
expected<void, std::error_code> - error if the OS rejected any configuration call. Definition at line 1026 of file thread_pool.hpp.
References threadschedule::detail::configure_worker_threads(), threadschedule::ThreadPriority::normal(), and threadschedule::OTHER.
|
inline |
Pin each worker to a distinct CPU core (round-robin).
Definition at line 1039 of file thread_pool.hpp.
References threadschedule::detail::distribute_workers_across_cpus().
|
inline |
Collect approximate performance counters.
Definition at line 979 of file thread_pool.hpp.
References threadschedule::HighPerformancePool::Statistics::active_threads, threadschedule::HighPerformancePool::Statistics::avg_task_time, threadschedule::HighPerformancePool::Statistics::completed_tasks, pending_tasks(), threadschedule::HighPerformancePool::Statistics::pending_tasks, threadschedule::HighPerformancePool::Statistics::stolen_tasks, threadschedule::HighPerformancePool::Statistics::tasks_per_second, and threadschedule::HighPerformancePool::Statistics::total_threads.
|
delete |
References HighPerformancePool().
|
inline |
Apply func to every element in [begin, end) in parallel.
The range is split into chunks and submitted as tasks. Blocks until all elements have been processed.
Definition at line 928 of file thread_pool.hpp.
References threadschedule::detail::parallel_for_each_chunked().
|
inlinenodiscard |
Approximate count of tasks waiting in all queues.
Definition at line 965 of file thread_pool.hpp.
Referenced by get_statistics().
|
inline |
Fire-and-forget task submission (throwing variant).
Enqueues a callable without creating a std::packaged_task or std::future, giving roughly 3x higher throughput than submit() for tasks whose return value is not needed.
| std::runtime_error | If the pool is shutting down. |
Definition at line 766 of file thread_pool.hpp.
References try_post().
|
inline |
Pin all workers to the same CPU set.
Definition at line 1033 of file thread_pool.hpp.
References threadschedule::detail::set_worker_affinity().
|
inline |
Definition at line 1095 of file thread_pool.hpp.
References threadschedule::detail::make_copyable_callable().
|
inline |
Register a callback invoked just after each task completes.
| cb | Receives the end time, the worker's std::thread::id, and the wall-clock duration of the task. |
Definition at line 1087 of file thread_pool.hpp.
|
inline |
Definition at line 1073 of file thread_pool.hpp.
References threadschedule::detail::make_copyable_callable().
|
inline |
Register a callback invoked just before each task executes.
| cb | Receives the start time and the worker's std::thread::id. |
Definition at line 1065 of file thread_pool.hpp.
|
inline |
Shut the pool down.
| policy | drain (default) finishes all queued tasks; drop_pending discards queued tasks. |
Definition at line 613 of file thread_pool.hpp.
References threadschedule::drain, and threadschedule::drop_pending.
Referenced by shutdown_for(), and ~HighPerformancePool().
|
inline |
Attempt a timed drain: finish as many tasks as possible within timeout, then force-stop remaining workers.
true if all tasks completed within the deadline, false if the timeout expired first. Definition at line 656 of file thread_pool.hpp.
References threadschedule::drain, and shutdown().
|
inlinenodiscardnoexcept |
Number of worker threads in this pool.
Definition at line 959 of file thread_pool.hpp.
|
inline |
Submit a task, throwing on shutdown.
Equivalent to try_submit but throws std::runtime_error instead of returning an error code when the pool is shutting down.
| std::runtime_error | If the pool is shutting down. |
std::future<R> that becomes ready when the task completes. Definition at line 747 of file thread_pool.hpp.
References try_submit().
|
inline |
Submit a range of void() callables in one go (throwing).
| std::runtime_error | If the pool is shutting down. |
Definition at line 913 of file thread_pool.hpp.
References try_submit_batch().
|
inline |
Fire-and-forget task submission (non-throwing variant).
expected<void, std::error_code> – std::errc::operation_canceled on shutdown. Definition at line 780 of file thread_pool.hpp.
References threadschedule::detail::bind_args(), and threadschedule::detail::make_move_callable().
Referenced by post().
|
inline |
Submit a task without throwing on shutdown.
Wraps the callable in a std::packaged_task and enqueues it. Returns an expected containing the std::future on success, or std::errc::operation_canceled if the pool is shutting down.
| F | Callable type. |
| Args | Argument types forwarded to F. |
| f | Callable to execute. |
| args | Arguments forwarded to f. |
expected<std::future<R>, std::error_code> where R = std::invoke_result_t<F, Args...>.Definition at line 691 of file thread_pool.hpp.
References threadschedule::detail::bind_args().
Referenced by submit().
|
inline |
Submit a range of void() callables in one go (non-throwing).
Acquires the lock once per batch, distributing tasks across worker queues in round-robin fashion. Significantly more efficient than calling submit() in a loop for large batches.
| Iterator | Forward iterator whose value_type is callable as void(). |
expected containing a vector of futures, or std::errc::operation_canceled on shutdown. Definition at line 866 of file thread_pool.hpp.
Referenced by submit_batch().
|
inline |
Block until all pending and active tasks have completed.
Definition at line 1050 of file thread_pool.hpp.