Class ThreadPool

Class Documentation

class quickpool::ThreadPool

A work stealing thread pool.

Public Functions

inline explicit ThreadPool(size_t threads = std::thread::hardware_concurrency())

constructs a thread pool.

Parameters

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

inline ~ThreadPool()
ThreadPool(ThreadPool&&) = delete
ThreadPool(ThreadPool const&) = delete
ThreadPool &operator=(ThreadPool const&) = delete
ThreadPool &operator=(ThreadPool &&other) = delete
inline void set_active_threads(size_t threads)

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

Parameters

threads – the number of worker threads. Has no effect when not called from owner thread.

inline size_t get_active_threads() const

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

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

pushes a job to the thread pool.

Parameters
  • f – a function.

  • args – (optional) arguments passed to f.

template<typename UnaryFunction>
inline void parallel_for(int begin, int end, UnaryFunction f)

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
  • begin – first index of the loop.

  • end – the loop runs in the range [begin, end).

  • f – a function taking int argument (the ‘loop body’).

template<typename Items, typename UnaryFunction>
inline void parallel_for_each(Items &items, UnaryFunction f)

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
  • items – an object allowing for std::begin() and std::end().

  • f – function to be applied as f(*it) for the iterator in the range [begin, end) (the ‘loop body’).

inline 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.

Parameters

millis – if > 0: stops waiting after millis ms.

inline bool done() const

checks whether all jobs are done.

Public Static Functions

static inline ThreadPool &global_instance()

returns a reference to the global thread pool instance.

static inline void *operator new(size_t count)

allocator respecting memory alignment.

static inline void operator delete(void *ptr)

deallocator respecting memory alignment.