quickpool  1.5.0
An easy-to-use, header-only work stealing thread pool in C++11
Public Member Functions | Static Public Member Functions | List of all members
quickpool::ThreadPool Class Reference

A work stealing thread pool. More...

#include <quickpool.hpp>

Public Member Functions

 ThreadPool (size_t threads=sched::num_cores_avail())
 constructs a thread pool. More...
 
 ThreadPool (ThreadPool &&)=delete
 
 ThreadPool (const ThreadPool &)=delete
 
ThreadPooloperator= (const ThreadPool &)=delete
 
ThreadPooloperator= (ThreadPool &&other)=delete
 
void set_active_threads (size_t threads)
 sets the number of active worker threads in the thread pool. More...
 
size_t get_active_threads () const
 retrieves the number of active worker threads in the thread pool.
 
template<class Function , class... Args>
void push (Function &&f, Args &&... args)
 pushes a job to the thread pool. More...
 
template<class Function , class... Args>
auto async (Function &&f, Args &&... args) -> std::future< decltype(f(args...))>
 executes a job asynchronously on the global thread pool. More...
 
template<class UnaryFunction >
void parallel_for (int begin, int end, UnaryFunction f)
 computes an index-based parallel for loop. More...
 
template<class Items , class UnaryFunction >
void parallel_for_each (Items &items, UnaryFunction f)
 computes a iterator-based parallel for loop. More...
 
void wait (size_t millis=0)
 waits for all jobs currently running on the thread pool. Has no effect when called from threads other than the one that created the pool. More...
 
bool done () const
 checks whether all jobs are done.
 

Static Public Member Functions

static ThreadPoolglobal_instance ()
 returns a reference to the global thread pool instance.
 
static void * operator new (size_t count)
 allocator respecting memory alignment.
 
static void operator delete (void *ptr)
 deallocator respecting memory alignment.
 

Detailed Description

A work stealing thread pool.

Definition at line 787 of file quickpool.hpp.

Constructor & Destructor Documentation

◆ ThreadPool()

quickpool::ThreadPool::ThreadPool ( size_t  threads = sched::num_cores_avail())
inlineexplicit

constructs a thread pool.

Parameters
threadsnumber of worker threads to create; defaults to number of available (virtual) hardware cores.

Definition at line 793 of file quickpool.hpp.

Member Function Documentation

◆ async()

template<class Function , class... Args>
auto quickpool::ThreadPool::async ( Function &&  f,
Args &&...  args 
) -> std::future<decltype(f(args...))>
inline

executes a job asynchronously on the global thread pool.

Parameters
fa function.
args(optional) arguments passed to f.
Returns
A std::future for the task. Call future.get() to retrieve the results at a later point in time (blocking).

Definition at line 872 of file quickpool.hpp.

◆ parallel_for()

template<class UnaryFunction >
void quickpool::ThreadPool::parallel_for ( int  begin,
int  end,
UnaryFunction  f 
)
inline

computes an index-based parallel for loop.

Waits until all tasks have finished, unless called from a thread that didn't create the pool. If this is taken into account, parallel loops can be nested.

Parameters
beginfirst index of the loop.
endthe loop runs in the range [begin, end).
fa function taking int argument (the 'loop body').

Definition at line 893 of file quickpool.hpp.

◆ parallel_for_each()

template<class Items , class UnaryFunction >
void quickpool::ThreadPool::parallel_for_each ( Items &  items,
UnaryFunction  f 
)
inline

computes a iterator-based parallel for loop.

Waits until all tasks have finished, unless called from a thread that didn't create the pool. If this is taken into account, parallel loops can be nested.

Parameters
itemsan object allowing for std::begin() and std::end().
ffunction to be applied as f(*it) for the iterator in the range [begin, end) (the 'loop body').

Definition at line 915 of file quickpool.hpp.

◆ push()

template<class Function , class... Args>
void quickpool::ThreadPool::push ( Function &&  f,
Args &&...  args 
)
inline

pushes a job to the thread pool.

Parameters
fa function.
args(optional) arguments passed to f.

Definition at line 858 of file quickpool.hpp.

◆ set_active_threads()

void quickpool::ThreadPool::set_active_threads ( size_t  threads)
inline

sets the number of active worker threads in the thread pool.

Parameters
threadsthe number of worker threads. Has no effect when not called from owner thread.

Definition at line 827 of file quickpool.hpp.

◆ wait()

void quickpool::ThreadPool::wait ( size_t  millis = 0)
inline

waits for all jobs currently running on the thread pool. Has no effect when called from threads other than the one that created the pool.

Parameters
millisif > 0: stops waiting after millis ms.

Definition at line 926 of file quickpool.hpp.


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