A library for working with phylogenetic data.
v0.25.0
ThreadPool Class Reference

#include <genesis/utils/core/thread_pool.hpp>

Detailed Description

Thread pool for distributed work.

This simple implementation offer a standing pool of worker threads that pick up tasks.

Example

// Create a thread pool with 2 worker threads
ThreadPool thread_pool( 2 );

// Enqueue a new task by providing a function and its arguments, and store its future result
auto result = thread_pool.enqueue(
    []( int some_param ) {
        // do computations
        int some_result = 42;
        return some_result;
    },
    0 // value for `some_param`
);

// Get the value of `some_result` from the future
// As this is a future, the function call to get() blocks until a thread has finished the work
std::cout << result.get() << "\n";

As the workers are stored in the ThreadPool object itself, it does not allow to be copied.

Definition at line 77 of file thread_pool.hpp.

Public Member Functions

 ThreadPool (size_t num_threads=0)
 Construct a thread pool with a given number of workers. More...
 
 ThreadPool (ThreadPool &&)=delete
 
 ThreadPool (ThreadPool const &)=delete
 
 ~ThreadPool ()
 Destruct the thread pool, stopping and joining any workers that are potentially still running or waiting. More...
 
template<class F , class... Args>
auto enqueue (F &&f, Args &&... args) -> std::future< typename std::result_of< F(Args...)>::type >
 Enqueue a new task, using a function to call and its arguments. More...
 
size_t load () const
 Return the current number of queued tasks. More...
 
ThreadPooloperator= (ThreadPool &&)=delete
 
ThreadPooloperator= (ThreadPool const &)=delete
 
size_t size () const
 Return the number of workers of the thread pool. More...
 

Constructor & Destructor Documentation

◆ ThreadPool() [1/3]

ThreadPool ( size_t  num_threads = 0)
inlineexplicit

Construct a thread pool with a given number of workers.

We allow for 0 tasks on construction, so that default construction is possible. However, the enqueue() throws if used with such an instance.

Definition at line 91 of file thread_pool.hpp.

◆ ThreadPool() [2/3]

ThreadPool ( ThreadPool const &  )
delete

◆ ThreadPool() [3/3]

ThreadPool ( ThreadPool &&  )
delete

◆ ~ThreadPool()

~ThreadPool ( )
inline

Destruct the thread pool, stopping and joining any workers that are potentially still running or waiting.

Definition at line 136 of file thread_pool.hpp.

Member Function Documentation

◆ enqueue()

auto enqueue ( F &&  f,
Args &&...  args 
) -> std::future<typename std::result_of<F(Args...)>::type>
inline

Enqueue a new task, using a function to call and its arguments.

The enqueue function returns a future that can be used to check whether the task has finished, or to wait for it to be finished. This also allows the task to send its result back to the caller, if needed, by simply returning it from the task function.

Definition at line 182 of file thread_pool.hpp.

◆ load()

size_t load ( ) const
inline

Return the current number of queued tasks.

Definition at line 168 of file thread_pool.hpp.

◆ operator=() [1/2]

ThreadPool& operator= ( ThreadPool &&  )
delete

◆ operator=() [2/2]

ThreadPool& operator= ( ThreadPool const &  )
delete

◆ size()

size_t size ( ) const
inline

Return the number of workers of the thread pool.

Definition at line 160 of file thread_pool.hpp.


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