ModErn Text Analysis
META Enumerates Textual Applications
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
meta::parallel::thread_pool Class Reference

Represents a collection of a fixed number of threads, which tasks can be added to. More...

#include <thread_pool.h>

Classes

struct  concrete_task
 A concrete task is templated with a result type. More...
 
struct  task
 A generic task object. More...
 

Public Member Functions

 thread_pool (size_t num_threads=std::thread::hardware_concurrency())
 
 ~thread_pool ()
 Destructor; joins all threads.
 
template<class Function >
std::future< typename std::result_of< Function()>::type > submit_task (Function func)
 Adds a task to the thread_pool. More...
 
std::vector< std::thread::id > thread_ids () const
 
size_t tasks () const
 

Private Member Functions

void worker ()
 Function invoked by the worker threads to process tasks off the internal queue.
 

Private Attributes

std::vector< std::thread > threads_
 the threads in the pool
 
std::queue< std::unique_ptr< task > > tasks_
 the queue containing the tasks to be run
 
bool running_
 whether or not the pool is currently running
 
std::mutex mutex_
 the mutex to wrap queue operations
 
std::condition_variable cond_
 the condition variable that workers sleep on when waiting for work
 

Detailed Description

Represents a collection of a fixed number of threads, which tasks can be added to.

Constructor & Destructor Documentation

meta::parallel::thread_pool::thread_pool ( size_t  num_threads = std::thread::hardware_concurrency())
inline
Parameters
num_threadsThe number of threads to initialize this thread_pool with; by default, the hardware concurrency.

Member Function Documentation

template<class Function >
std::future<typename std::result_of<Function()>::type> meta::parallel::thread_pool::submit_task ( Function  func)
inline

Adds a task to the thread_pool.

Parameters
funcThe function (task) to add
Returns
a std::future that wraps the return value of the task for retrieval later
std::vector<std::thread::id> meta::parallel::thread_pool::thread_ids ( ) const
inline
Returns
a vector of the thread_ids from the current pool
size_t meta::parallel::thread_pool::tasks ( ) const
inline
Returns
the number of currently queued tasks

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