ThreadSchedule 2.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
threadschedule::ThreadPoolBase< WaitPolicy > Class Template Reference

Single-queue thread pool parameterized by its idle-wait strategy. More...

#include <thread_pool.hpp>

Inheritance diagram for threadschedule::ThreadPoolBase< WaitPolicy >:
[legend]

Classes

struct  Statistics

Public Types

using Task = std::function<void()>

Public Member Functions

 ThreadPoolBase (size_t num_threads=std::thread::hardware_concurrency(), bool register_workers=false)
 ThreadPoolBase (ThreadPoolBase const &)=delete
auto operator= (ThreadPoolBase const &) -> ThreadPoolBase &=delete
 ~ThreadPoolBase ()
Task submission
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 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, 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 & lifecycle
void wait_for_tasks ()
 Block until all pending and active tasks have completed.
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.
Tracing hooks
void set_on_task_start (TaskStartCallback cb)
 Register a callback invoked just before each task executes.
void set_on_task_end (TaskEndCallback cb)
 Register a callback invoked just after each task completes.

Detailed Description

template<typename WaitPolicy>
class threadschedule::ThreadPoolBase< WaitPolicy >

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:

  • IndefiniteWait - blocks on condition_variable::wait() (zero CPU while idle, instant wake). Instantiated as ThreadPool.
  • PollingWait - polls with condition_variable::wait_for(10 ms). Slightly higher idle CPU but lower worst-case latency under bursty loads. Instantiated as FastThreadPool.
How task execution works
When you call submit(), the callable is wrapped in a std::packaged_task, pushed into the shared task queue under a mutex lock, and one sleeping worker is woken via condition_variable::notify_one(). The woken worker pops the front element and executes it.
Execution guarantees
  • Every successfully submitted task (submit() returned without throwing) is guaranteed to eventually execute.
  • submit() throws std::runtime_error if the pool is already shutting down. In that case the task is NOT enqueued.
  • Tasks are stored in a FIFO queue. Multiple workers pop concurrently, so submission order is roughly preserved but completion order is non-deterministic.
  • The returned std::future becomes ready once the task finishes. If the task threw an exception, future.get() rethrows it.
  • On shutdown(), workers finish their current task, then drain all remaining queued tasks before exiting.
  • wait_for_tasks() blocks until the queue is empty AND no worker is currently executing a task.
Thread safety
submit() and submit_batch() may be called from any thread concurrently. shutdown() is internally guarded and safe to call more than once.
Exception handling
Exceptions thrown by tasks are caught inside the worker loop. They are stored in the std::future returned by submit(). The worker thread continues processing.
Lifetime
The destructor calls shutdown() and joins all worker threads. Can block if tasks are still running.
Copyability / movability
Not copyable, not movable.
Template Parameters
WaitPolicyStrategy type with a static wait(cv, lock, predicate) -> bool method.

Definition at line 1266 of file thread_pool.hpp.

Member Typedef Documentation

◆ Task

template<typename WaitPolicy>
using threadschedule::ThreadPoolBase< WaitPolicy >::Task = std::function<void()>

Definition at line 1269 of file thread_pool.hpp.

Constructor & Destructor Documentation

◆ ThreadPoolBase() [1/2]

template<typename WaitPolicy>
threadschedule::ThreadPoolBase< WaitPolicy >::ThreadPoolBase ( size_t num_threads = std::thread::hardware_concurrency(),
bool register_workers = false )
inlineexplicit

Definition at line 1281 of file thread_pool.hpp.

◆ ThreadPoolBase() [2/2]

template<typename WaitPolicy>
threadschedule::ThreadPoolBase< WaitPolicy >::ThreadPoolBase ( ThreadPoolBase< WaitPolicy > const & )
delete

◆ ~ThreadPoolBase()

template<typename WaitPolicy>
threadschedule::ThreadPoolBase< WaitPolicy >::~ThreadPoolBase ( )
inline

Definition at line 1296 of file thread_pool.hpp.

Member Function Documentation

◆ configure_threads()

template<typename WaitPolicy>
auto threadschedule::ThreadPoolBase< WaitPolicy >::configure_threads ( std::string const & name_prefix,
SchedulingPolicy policy = SchedulingPolicy::OTHER,
ThreadPriority priority = ThreadPriority::normal() ) -> expected<void, std::error_code>
inline

Name, schedule and prioritize all worker threads.

See also
HighPerformancePool::configure_threads

Definition at line 1503 of file thread_pool.hpp.

◆ distribute_across_cpus()

template<typename WaitPolicy>
auto threadschedule::ThreadPoolBase< WaitPolicy >::distribute_across_cpus ( ) -> expected<void, std::error_code>
inline

Pin each worker to a distinct CPU core (round-robin).

Definition at line 1516 of file thread_pool.hpp.

◆ get_statistics()

template<typename WaitPolicy>
auto threadschedule::ThreadPoolBase< WaitPolicy >::get_statistics ( ) const -> Statistics
inlinenodiscard

Collect approximate performance counters.

Definition at line 1595 of file thread_pool.hpp.

◆ operator=()

template<typename WaitPolicy>
auto threadschedule::ThreadPoolBase< WaitPolicy >::operator= ( ThreadPoolBase< WaitPolicy > const & ) -> ThreadPoolBase &=delete
delete

◆ parallel_for_each()

template<typename WaitPolicy>
template<typename Iterator, typename F>
void threadschedule::ThreadPoolBase< WaitPolicy >::parallel_for_each ( Iterator begin,
Iterator end,
F && func )
inline

Apply func to [begin, end) in parallel (chunked).

Definition at line 1449 of file thread_pool.hpp.

◆ pending_tasks()

template<typename WaitPolicy>
auto threadschedule::ThreadPoolBase< WaitPolicy >::pending_tasks ( ) const -> size_t
inlinenodiscard

Number of tasks waiting in the queue.

Definition at line 1488 of file thread_pool.hpp.

◆ post()

template<typename WaitPolicy>
template<typename F, typename... Args>
void threadschedule::ThreadPoolBase< WaitPolicy >::post ( F && f,
Args &&... args )
inline

Fire-and-forget task submission (throwing variant).

Bypasses std::packaged_task / std::future for lower overhead.

Exceptions
std::runtime_errorIf the pool is shutting down.
See also
try_post()

Definition at line 1352 of file thread_pool.hpp.

◆ set_affinity()

template<typename WaitPolicy>
auto threadschedule::ThreadPoolBase< WaitPolicy >::set_affinity ( ThreadAffinity const & affinity) -> expected<void, std::error_code>
inline

Pin all workers to the same CPU set.

Definition at line 1510 of file thread_pool.hpp.

◆ set_on_task_end()

template<typename WaitPolicy>
void threadschedule::ThreadPoolBase< WaitPolicy >::set_on_task_end ( TaskEndCallback cb)
inline

Register a callback invoked just after each task completes.

Parameters
cbReceives the end time, the worker's std::thread::id, and the wall-clock duration of the task.

Definition at line 1649 of file thread_pool.hpp.

◆ set_on_task_start()

template<typename WaitPolicy>
void threadschedule::ThreadPoolBase< WaitPolicy >::set_on_task_start ( TaskStartCallback cb)
inline

Register a callback invoked just before each task executes.

Parameters
cbReceives the start time and the worker's std::thread::id.

Definition at line 1638 of file thread_pool.hpp.

◆ shutdown()

template<typename WaitPolicy>
void threadschedule::ThreadPoolBase< WaitPolicy >::shutdown ( ShutdownPolicy policy = ShutdownPolicy::drain)
inline

Shut the pool down.

Parameters
policydrain (default) finishes all queued tasks; drop_pending discards queued tasks.

Definition at line 1539 of file thread_pool.hpp.

◆ shutdown_for()

template<typename WaitPolicy>
auto threadschedule::ThreadPoolBase< WaitPolicy >::shutdown_for ( std::chrono::milliseconds timeout) -> bool
inline

Attempt a timed drain: finish as many tasks as possible within timeout, then force-stop remaining workers.

Returns
true if all tasks completed within the deadline, false if the timeout expired first.

Definition at line 1570 of file thread_pool.hpp.

◆ size()

template<typename WaitPolicy>
auto threadschedule::ThreadPoolBase< WaitPolicy >::size ( ) const -> size_t
inlinenodiscardnoexcept

Number of worker threads.

Definition at line 1482 of file thread_pool.hpp.

◆ submit()

template<typename WaitPolicy>
template<typename F, typename... Args>
auto threadschedule::ThreadPoolBase< WaitPolicy >::submit ( F && f,
Args &&... args ) -> std::future<std::invoke_result_t<F, Args...>>
inline

Submit a task, throwing on shutdown.

Exceptions
std::runtime_errorIf the pool is shutting down.

Definition at line 1335 of file thread_pool.hpp.

◆ submit_batch()

template<typename WaitPolicy>
template<typename Iterator>
auto threadschedule::ThreadPoolBase< WaitPolicy >::submit_batch ( Iterator begin,
Iterator end ) -> std::vector<std::future<void>>
inline

Submit a batch of tasks (throwing).

See also
try_submit_batch()

Definition at line 1439 of file thread_pool.hpp.

◆ try_post()

template<typename WaitPolicy>
template<typename F, typename... Args>
auto threadschedule::ThreadPoolBase< WaitPolicy >::try_post ( F && f,
Args &&... args ) -> expected<void, std::error_code>
inline

Fire-and-forget task submission (non-throwing variant).

Returns
expected<void, std::error_code>std::errc::operation_canceled on shutdown.

Definition at line 1365 of file thread_pool.hpp.

◆ try_submit()

template<typename WaitPolicy>
template<typename F, typename... Args>
auto threadschedule::ThreadPoolBase< WaitPolicy >::try_submit ( F && f,
Args &&... args ) -> expected<std::future<std::invoke_result_t<F, Args...>>, std::error_code>
inline

Submit a task without throwing on shutdown.

Returns
expected<std::future<R>, std::error_code>.
See also
submit() for the throwing variant.

Definition at line 1310 of file thread_pool.hpp.

◆ try_submit_batch()

template<typename WaitPolicy>
template<typename Iterator>
auto threadschedule::ThreadPoolBase< WaitPolicy >::try_submit_batch ( Iterator begin,
Iterator end ) -> expected<std::vector<std::future<void>>, std::error_code>
inline

Submit a range of void() callables in one go (non-throwing).

All tasks are enqueued under a single lock acquisition.

Definition at line 1415 of file thread_pool.hpp.

◆ wait_for_tasks()

template<typename WaitPolicy>
void threadschedule::ThreadPoolBase< WaitPolicy >::wait_for_tasks ( )
inline

Block until all pending and active tasks have completed.

Definition at line 1527 of file thread_pool.hpp.


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