ThreadSchedule 1.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
threadschedule::ThreadPool Class Reference

Simple thread pool for general-purpose use. More...

#include <thread_pool.hpp>

Classes

struct  Statistics

Public Types

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

Public Member Functions

 ThreadPool (size_t num_threads=std::thread::hardware_concurrency())
 ThreadPool (ThreadPool const &)=delete
auto operator= (ThreadPool const &) -> ThreadPool &=delete
template<typename F, typename... Args>
auto submit (F &&f, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > >
 Submit a task to the thread pool.
template<typename Iterator>
auto submit_range (Iterator begin, Iterator end) -> std::vector< std::future< void > >
 Submit multiple tasks.
template<typename Iterator, typename F>
void parallel_for_each (Iterator begin, Iterator end, F &&func)
 Apply a function to a range of values in parallel.
auto size () const noexcept -> size_t
auto pending_tasks () const -> size_t
auto configure_threads (std::string const &name_prefix, SchedulingPolicy policy=SchedulingPolicy::OTHER, ThreadPriority priority=ThreadPriority::normal()) -> bool
 Configure thread properties.
auto set_affinity (ThreadAffinity const &affinity) -> bool
auto distribute_across_cpus () -> bool
void wait_for_tasks ()
void shutdown ()
auto get_statistics () const -> Statistics

Detailed Description

Simple thread pool for general-purpose use.

This is a straightforward thread pool implementation suitable for:

  • Simple workloads with low task counts (< 1k tasks)
  • General application use (50k-500k tasks/second)
  • Simple task submission patterns
  • Lower memory overhead and complexity
  • Easier to understand and debug

For high-throughput scenarios (> 1k tasks), consider FastThreadPool or HighPerformancePool.

Definition at line 887 of file thread_pool.hpp.

Member Typedef Documentation

◆ Task

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

Definition at line 890 of file thread_pool.hpp.

Constructor & Destructor Documentation

◆ ThreadPool()

threadschedule::ThreadPool::ThreadPool ( size_t num_threads = std::thread::hardware_concurrency())
inlineexplicit

Definition at line 900 of file thread_pool.hpp.

◆ ~ThreadPool()

threadschedule::ThreadPool::~ThreadPool ( )
inline

Definition at line 915 of file thread_pool.hpp.

Member Function Documentation

◆ configure_threads()

auto threadschedule::ThreadPool::configure_threads ( std::string const & name_prefix,
SchedulingPolicy policy = SchedulingPolicy::OTHER,
ThreadPriority priority = ThreadPriority::normal() ) -> bool
inline

Configure thread properties.

Definition at line 1000 of file thread_pool.hpp.

◆ distribute_across_cpus()

auto threadschedule::ThreadPool::distribute_across_cpus ( ) -> bool
inline

Definition at line 1038 of file thread_pool.hpp.

◆ get_statistics()

auto threadschedule::ThreadPool::get_statistics ( ) const -> Statistics
inline

Definition at line 1086 of file thread_pool.hpp.

◆ parallel_for_each()

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

Apply a function to a range of values in parallel.

Definition at line 969 of file thread_pool.hpp.

References submit().

◆ pending_tasks()

auto threadschedule::ThreadPool::pending_tasks ( ) const -> size_t
inline

Definition at line 991 of file thread_pool.hpp.

◆ set_affinity()

auto threadschedule::ThreadPool::set_affinity ( ThreadAffinity const & affinity) -> bool
inline

Definition at line 1023 of file thread_pool.hpp.

◆ shutdown()

void threadschedule::ThreadPool::shutdown ( )
inline

Definition at line 1064 of file thread_pool.hpp.

◆ size()

auto threadschedule::ThreadPool::size ( ) const -> size_t
inlinenoexcept

Definition at line 986 of file thread_pool.hpp.

◆ submit()

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

Submit a task to the thread pool.

Definition at line 924 of file thread_pool.hpp.

Referenced by parallel_for_each(), and submit_range().

◆ submit_range()

template<typename Iterator>
auto threadschedule::ThreadPool::submit_range ( Iterator begin,
Iterator end ) -> std::vector<std::future<void>>
inline

Submit multiple tasks.

Definition at line 952 of file thread_pool.hpp.

References submit().

◆ wait_for_tasks()

void threadschedule::ThreadPool::wait_for_tasks ( )
inline

Definition at line 1058 of file thread_pool.hpp.


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