|
ThreadSchedule 2.0.0
Modern C++ thread management library
|
Ultra-lightweight fire-and-forget thread pool. More...
#include <thread_pool.hpp>
Public Member Functions | |
| LightweightPoolT (size_t num_threads=std::thread::hardware_concurrency()) | |
Construct a lightweight pool with num_threads workers. | |
| LightweightPoolT (LightweightPoolT const &)=delete | |
| auto | operator= (LightweightPoolT const &) -> LightweightPoolT &=delete |
| ~LightweightPoolT () | |
Task submission | |
| template<typename F, typename... Args> | |
| void | post (F &&f, Args &&... args) |
| Post a fire-and-forget task (throwing variant). | |
| template<typename F, typename... Args> | |
| auto | try_post (F &&f, Args &&... args) -> expected< void, std::error_code > |
| Post a fire-and-forget task (non-throwing variant). | |
| template<typename Iterator> | |
| void | post_batch (Iterator begin, Iterator end) |
| Post a range of callables under a single lock acquisition. | |
| template<typename Iterator> | |
| auto | try_post_batch (Iterator begin, Iterator end) -> expected< void, std::error_code > |
| Batch post (non-throwing). | |
Lifecycle | |
| void | shutdown (ShutdownPolicy policy=ShutdownPolicy::drain) |
| Shut the pool down. | |
| auto | shutdown_for (std::chrono::milliseconds timeout) -> bool |
| Attempt a timed drain. | |
Observers | |
| auto | size () const noexcept -> size_t |
| Number of worker threads. | |
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). | |
Ultra-lightweight fire-and-forget thread pool.
Designed for maximum throughput on tasks whose return value is not needed. Typical measured throughput is 3x higher than submit() on e.g. HighPerformancePool on the same hardware, because LightweightPoolT avoids the overhead of std::packaged_task, std::future, and std::shared_ptr entirely.
std::queue of detail::SboCallable objects protected by one mutex + condition_variable.TaskSize - 8 bytes are stored inline (no heap allocation). Larger callables fall back to the heap.std::future / std::packaged_task (use submit() on other pools if you need return values).ThreadRegistry auto-registration.shutdown(ShutdownPolicy::drop_pending) is called).post(), try_post(), post_batch(), and try_post_batch() may be called from any number of threads concurrently. shutdown() is internally guarded and safe to call more than once.shutdown(ShutdownPolicy::drain) and joins all workers. It blocks until every queued task has been executed.TaskSize TaskSize to avoid the heap fallback: | TaskSize | Total size in bytes of each detail::SboCallable slot (default 64). Usable inline buffer = TaskSize - 8 bytes on 64-bit platforms. |
LightweightPoolT<64>), ScheduledLightweightPool (scheduled variant). Definition at line 1846 of file thread_pool.hpp.
|
inlineexplicit |
Construct a lightweight pool with num_threads workers.
| num_threads | Number of worker threads (clamped to at least 1). Defaults to std::thread::hardware_concurrency(). |
Definition at line 1854 of file thread_pool.hpp.
Referenced by LightweightPoolT(), and operator=().
|
delete |
References LightweightPoolT().
|
inline |
Definition at line 1865 of file thread_pool.hpp.
References threadschedule::drain, and shutdown().
|
inline |
Name, schedule and prioritize all worker threads.
Workers are named name_prefix + "_0", "_1", etc.
Definition at line 2046 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 2059 of file thread_pool.hpp.
References threadschedule::detail::distribute_workers_across_cpus().
|
delete |
References LightweightPoolT().
|
inline |
Post a fire-and-forget task (throwing variant).
The callable and its arguments are bound into a detail::SboCallable and pushed into the shared queue.
| F | Callable type. |
| Args | Argument types forwarded to F. |
| std::runtime_error | If the pool is shutting down. |
Definition at line 1885 of file thread_pool.hpp.
References try_post().
|
inline |
Post a range of callables under a single lock acquisition.
More efficient than calling post() in a loop because the mutex is acquired only once and all workers are woken via notify_all().
| Iterator | Forward iterator whose value_type is callable as void(). |
| std::runtime_error | If the pool is shutting down. |
Definition at line 1922 of file thread_pool.hpp.
References try_post_batch().
|
inline |
Pin all workers to the same CPU set.
Definition at line 2053 of file thread_pool.hpp.
References threadschedule::detail::set_worker_affinity().
|
inline |
Shut the pool down.
| policy | drain (default) - workers finish all queued tasks before exiting. drop_pending - the queue is cleared and only the currently executing tasks are allowed to finish. |
Safe to call more than once (subsequent calls are no-ops).
Definition at line 1978 of file thread_pool.hpp.
References threadschedule::drain, and threadschedule::drop_pending.
Referenced by shutdown_for(), and ~LightweightPoolT().
|
inline |
Attempt a timed drain.
Waits up to timeout for all tasks to complete, then performs a full shutdown(drain).
true if all tasks completed within the deadline, false if the timeout expired (pool is still shut down). Definition at line 2009 of file thread_pool.hpp.
References threadschedule::drain, and shutdown().
|
inlinenodiscardnoexcept |
Number of worker threads.
Definition at line 2031 of file thread_pool.hpp.
|
inline |
Post a fire-and-forget task (non-throwing variant).
expected<void, std::error_code> – std::errc::operation_canceled on shutdown. Definition at line 1899 of file thread_pool.hpp.
References threadschedule::detail::bind_args().
Referenced by post().
|
inline |
Batch post (non-throwing).
expected<void, std::error_code>. Definition at line 1934 of file thread_pool.hpp.
Referenced by post_batch().