HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
thread_pool Class Reference

#include <thread.h>

Public Member Functions

 thread_pool (int nthreads=-1)
 
 ~thread_pool ()
 
int size () const
 How many threads are in the pool? More...
 
void resize (int nthreads=-1)
 
int idle () const
 
template<typename F >
auto push (F &&f) -> std::future< decltype(f(0))>
 
template<typename F , typename... Rest>
auto push (F &&f, Rest &&...rest) -> std::future< decltype(f(0, rest...))>
 
bool run_one_task (std::thread::id id)
 
bool this_thread_is_in_pool () const
 
void register_worker (std::thread::id id)
 
void deregister_worker (std::thread::id id)
 
bool is_worker (std::thread::id id) const
 
bool is_worker () const
 
bool is_worker (std::thread::id id)
 
size_t jobs_in_queue () const
 
bool very_busy () const
 

Detailed Description

Definition at line 644 of file thread.h.

Constructor & Destructor Documentation

thread_pool::thread_pool ( int  nthreads = -1)

Initialize the pool. This implicitly calls resize() to set the number of worker threads, defaulting to a number of workers that is one less than the number of hardware cores.

thread_pool::~thread_pool ( )

Member Function Documentation

void thread_pool::deregister_worker ( std::thread::id  id)

De-register a thread, saying it is no longer in the process of taking work from the thread pool.

int thread_pool::idle ( ) const

Return the number of currently idle threads in the queue. Zero means the queue is fully engaged.

bool thread_pool::is_worker ( std::thread::id  id) const

Is the thread in the pool or currently engaged in taking tasks from the pool?

bool thread_pool::is_worker ( ) const
inline

Definition at line 730 of file thread.h.

bool thread_pool::is_worker ( std::thread::id  id)
size_t thread_pool::jobs_in_queue ( ) const

How many jobs are waiting to run? (Use with caution! Can be out of date by the time you look at it.)

template<typename F >
auto thread_pool::push ( F &&  f) -> std::future<decltype(f(0))>
inline

Run the user's function that accepts argument int - id of the running thread. The returned value is templatized std::future, where the user can get the result and rethrow any exceptions. If the queue has no worker threads, the task will be run immediately by the calling thread.

Definition at line 672 of file thread.h.

template<typename F , typename... Rest>
auto thread_pool::push ( F &&  f,
Rest &&...  rest 
) -> std::future<decltype(f(0, rest...))>
inline

Run the user's function that accepts an arbitrary set of arguments (also passed). The returned value is templatized std::future, where the user can get the result and rethrow any exceptions. If the queue has no worker threads, the task will be run immediately by the calling thread.

Definition at line 692 of file thread.h.

void thread_pool::register_worker ( std::thread::id  id)

Register a thread (not already in the thread pool itself) as working on tasks in the pool. This is used to avoid recursion.

void thread_pool::resize ( int  nthreads = -1)

Sets the number of worker threads in the pool. If the pool size is 0, any tasks added to the pool will be executed immediately by the calling thread. Requesting nthreads < 0 will cause it to resize to the number of hardware cores minus one (one less, to account for the fact that the calling thread will also contribute). BEWARE! Resizing the queue should not be done while jobs are running.

bool thread_pool::run_one_task ( std::thread::id  id)

If there are any tasks on the queue, pull one off and run it (on this calling thread) and return true. Otherwise (there are no pending jobs), return false immediately. This utility is what makes it possible for non-pool threads to also run tasks from the queue when they would ordinarily be idle. The thread id of the caller should be passed.

int thread_pool::size ( ) const

How many threads are in the pool?

bool thread_pool::this_thread_is_in_pool ( ) const

Return true if the calling thread is part of the thread pool. This can be used to limit a pool thread from unadvisedly adding its own subtasks to clog up the pool. DEPRECATED(2.1) – use is_worker() instead.

bool thread_pool::very_busy ( ) const

Is the pool very busy? Meaning that there are significantly more tasks in the queue waiting to run than there are threads in the pool. It may be wise for a caller to check this before submitting tasks – if the queue is very busy, it's probably more expedient to execute the code directly rather than add it to an oversubscribed queue.


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