|
| | ThreadPoolBase (size_t num_threads=std::thread::hardware_concurrency(), bool register_workers=false) |
| | ThreadPoolBase (ThreadPoolBase const &)=delete |
| auto | operator= (ThreadPoolBase const &) -> ThreadPoolBase &=delete |
| | ~ThreadPoolBase () |
| 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).
|
| 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.
|
| 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).
|
| 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.
|
| 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.
|
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
-
| WaitPolicy | Strategy type with a static wait(cv, lock, predicate) -> bool method. |
Definition at line 1266 of file thread_pool.hpp.