|
ThreadSchedule 3.0.0
Modern C++ thread management library
|
Single-queue thread pool parameterized by its idle-wait strategy. More...
#include <thread_pool_backend_base.hpp>
Classes | |
| struct | statistics |
Public Types | |
| using | task_type = std::function< void()> |
| using | queued_task = detail::move_only_function< void()> |
Public Member Functions | |
| thread_pool_backend_base (size_t num_threads=default_worker_count(), bool register_workers=false) | |
| thread_pool_backend_base (thread_pool_backend_base const &)=delete | |
| auto | operator= (thread_pool_backend_base const &) -> thread_pool_backend_base &=delete |
| ~thread_pool_backend_base () | |
Task submission | |
| template<typename F , typename... Args> | |
| auto | try_submit (F &&f, Args &&... args) -> expected< std::future< bind_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< bind_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 batch of tasks (throwing). | |
| template<typename Iterator , typename F > | |
| void | parallel_for_each (Iterator begin, Iterator end, F &&func) |
Apply func to [begin, end) in parallel (chunked). | |
Observers | |
| auto | size () const noexcept -> size_t |
| Number of worker threads. | |
| auto | pending_tasks () const -> size_t |
| Number of tasks waiting in the queue. | |
| auto | get_statistics () const -> statistics |
| Collect approximate performance counters. | |
Thread configuration | |
| auto | configure_threads (std::string const &name_prefix, native_scheduling_policy policy=native_scheduling_policy::other, native_thread_priority priority=native_thread_priority::normal()) -> expected< void, std::error_code > |
| Name, schedule and prioritize all worker threads. | |
| auto | configure_threads (native_thread_config const &config) -> expected< void, std::error_code > |
| auto | set_affinity (native_thread_affinity 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 & lifecycle | |
| void | wait_for_tasks () |
| Block until all pending and active tasks have completed. | |
| auto | is_current_worker () const noexcept -> bool |
| void | shutdown (shutdown_policy_backend policy=shutdown_policy_backend::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 discard queued work. | |
Tracing hooks | |
| void | set_on_task_start (task_start_callback 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 >, task_start_callback >, int > = 0> | |
| void | set_on_task_start (Callback &&cb) |
| void | set_on_task_end (task_end_callback 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 >, task_end_callback >, int > = 0> | |
| void | set_on_task_end (Callback &&cb) |
Single-queue thread pool parameterized by its idle-wait strategy.
All tasks share one std::queue protected by a single mutex. The WaitPolicy template parameter controls how workers wait for new work:
thread_pool_backend.polling_pool_backend.| WaitPolicy | Strategy type with a static wait(cv, lock, predicate) -> bool method. |
Definition at line 97 of file thread_pool_backend_base.hpp.
| using threadschedule::detail::thread_pool_backend_base< WaitPolicy >::queued_task = detail::move_only_function<void()> |
Definition at line 101 of file thread_pool_backend_base.hpp.
| using threadschedule::detail::thread_pool_backend_base< WaitPolicy >::task_type = std::function<void()> |
Definition at line 100 of file thread_pool_backend_base.hpp.
|
inlineexplicit |
Definition at line 113 of file thread_pool_backend_base.hpp.
References threadschedule::detail::worker_startup_latch::wait().
|
delete |
|
inline |
Definition at line 139 of file thread_pool_backend_base.hpp.
References threadschedule::detail::drain, and threadschedule::detail::thread_pool_backend_base< WaitPolicy >::shutdown().
|
inline |
Definition at line 333 of file thread_pool_backend_base.hpp.
References threadschedule::detail::configure_worker_threads(), and threadschedule::detail::runtime_registry().
|
inline |
Name, schedule and prioritize all worker threads.
Definition at line 324 of file thread_pool_backend_base.hpp.
References threadschedule::detail::configure_worker_threads(), and threadschedule::detail::runtime_registry().
|
inline |
Pin each worker to a distinct CPU core (round-robin).
Definition at line 347 of file thread_pool_backend_base.hpp.
References threadschedule::detail::distribute_workers_across_cpus().
|
inline |
Collect approximate performance counters.
Definition at line 475 of file thread_pool_backend_base.hpp.
References threadschedule::detail::thread_pool_backend_base< WaitPolicy >::statistics::active_threads, threadschedule::detail::thread_pool_backend_base< WaitPolicy >::statistics::avg_task_time, threadschedule::detail::thread_pool_backend_base< WaitPolicy >::statistics::completed_tasks, threadschedule::detail::thread_pool_backend_base< WaitPolicy >::statistics::pending_tasks, threadschedule::detail::thread_pool_backend_base< WaitPolicy >::statistics::tasks_per_second, and threadschedule::detail::thread_pool_backend_base< WaitPolicy >::statistics::total_threads.
|
inlinenoexcept |
Definition at line 369 of file thread_pool_backend_base.hpp.
Referenced by threadschedule::detail::thread_pool_backend_base< WaitPolicy >::parallel_for_each(), threadschedule::detail::thread_pool_backend_base< WaitPolicy >::shutdown(), threadschedule::detail::thread_pool_backend_base< WaitPolicy >::shutdown_for(), and threadschedule::detail::thread_pool_backend_base< WaitPolicy >::wait_for_tasks().
|
delete |
|
inline |
Apply func to [begin, end) in parallel (chunked).
| Iterator | Forward iterator; task chunks retain iterator pairs. |
Definition at line 287 of file thread_pool_backend_base.hpp.
References threadschedule::detail::thread_pool_backend_base< WaitPolicy >::is_current_worker(), threadschedule::detail::parallel_for_each_chunked(), and threadschedule::detail::throw_worker_deadlock().
|
inline |
Number of tasks waiting in the queue.
Definition at line 308 of file thread_pool_backend_base.hpp.
|
inline |
Fire-and-forget task submission (throwing variant).
Bypasses std::packaged_task / std::future for lower overhead.
| std::runtime_error | If the pool is shutting down. |
Definition at line 198 of file thread_pool_backend_base.hpp.
References threadschedule::detail::thread_pool_backend_base< WaitPolicy >::try_post().
|
inline |
Pin all workers to the same CPU set.
Definition at line 340 of file thread_pool_backend_base.hpp.
References threadschedule::detail::set_worker_affinity().
|
inline |
Definition at line 552 of file thread_pool_backend_base.hpp.
References threadschedule::detail::make_copyable_function().
|
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 543 of file thread_pool_backend_base.hpp.
|
inline |
Definition at line 528 of file thread_pool_backend_base.hpp.
|
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 519 of file thread_pool_backend_base.hpp.
|
inline |
Shut the pool down.
| policy | drain (default) finishes all queued tasks; drop_pending discards queued tasks. |
Definition at line 380 of file thread_pool_backend_base.hpp.
References threadschedule::detail::drop_pending, threadschedule::detail::thread_pool_backend_base< WaitPolicy >::is_current_worker(), and threadschedule::detail::throw_worker_deadlock().
Referenced by threadschedule::detail::thread_pool_backend_base< WaitPolicy >::~thread_pool_backend_base().
|
inline |
Attempt a timed drain: finish as many tasks as possible within timeout, then discard queued work.
New submissions are rejected before the timed wait begins. Running C++ callables cannot be stopped safely, so this function can return after the timeout while an already-running task finishes.
true if all tasks completed within the deadline, false if the timeout expired first. Definition at line 425 of file thread_pool_backend_base.hpp.
References threadschedule::detail::thread_pool_backend_base< WaitPolicy >::is_current_worker(), threadschedule::detail::shutdown_deadline_after(), and threadschedule::detail::throw_worker_deadlock().
|
inlinenoexcept |
Number of worker threads.
Definition at line 301 of file thread_pool_backend_base.hpp.
|
inline |
Submit a task, throwing on shutdown.
| std::runtime_error | If the pool is shutting down. |
Definition at line 180 of file thread_pool_backend_base.hpp.
References threadschedule::expected< T, E >::has_value(), threadschedule::detail::thread_pool_backend_base< WaitPolicy >::try_submit(), and threadschedule::expected< T, E >::value().
|
inline |
Submit a batch of tasks (throwing).
Definition at line 275 of file thread_pool_backend_base.hpp.
References threadschedule::expected< T, E >::has_value(), threadschedule::detail::thread_pool_backend_base< WaitPolicy >::try_submit_batch(), and threadschedule::expected< T, E >::value().
|
inline |
Fire-and-forget task submission (non-throwing variant).
expected<void, std::error_code> – std::errc::operation_canceled on shutdown. Definition at line 212 of file thread_pool_backend_base.hpp.
References threadschedule::detail::bind_args().
Referenced by threadschedule::detail::thread_pool_backend_base< WaitPolicy >::post().
|
inline |
Submit a task without throwing on shutdown.
expected<std::future<R>, std::error_code>. Definition at line 154 of file thread_pool_backend_base.hpp.
References threadschedule::detail::bind_args().
Referenced by threadschedule::detail::thread_pool_backend_base< WaitPolicy >::submit().
|
inline |
Submit a range of void() callables in one go (non-throwing).
All tasks are enqueued under a single lock acquisition.
| Iterator | Input iterator whose value is callable as void(). |
Definition at line 233 of file thread_pool_backend_base.hpp.
References threadschedule::detail::multipass_range_size().
Referenced by threadschedule::detail::thread_pool_backend_base< WaitPolicy >::submit_batch().
|
inline |
Block until all pending and active tasks have completed.
Definition at line 359 of file thread_pool_backend_base.hpp.
References threadschedule::detail::thread_pool_backend_base< WaitPolicy >::is_current_worker(), and threadschedule::detail::throw_worker_deadlock().