OKVIS ROS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
okvis::ThreadPool Class Reference

This class manages multiple threads and fills them with work. More...

#include <ThreadPool.hpp>

Public Member Functions

 ThreadPool (size_t numThreads)
 Constructor. Launches some amount of workers. More...
 
 ~ThreadPool ()
 Destructor. This joins all threads. More...
 
template<class Function , class... Args>
std::future< typename
std::result_of< Function(Args...)>
::type > 
enqueue (Function &&function, Args &&...args)
 Enqueue work for the thread pool. More...
 
void stop ()
 Stop the thread pool. This method is non-blocking. More...
 
void waitForEmptyQueue () const
 This method blocks until the queue is empty. More...
 

Private Member Functions

void run ()
 Run a single thread. More...
 

Private Attributes

std::vector< std::thread > workers_
 Need to keep track of threads so we can join them. More...
 
std::queue< std::function
< void()> > 
tasks_
 The task queue. More...
 
std::mutex tasks_mutex_
 A mutex to protect the list of tasks. More...
 
std::condition_variable tasks_condition_
 A condition variable for worker threads. More...
 
std::condition_variable wait_condition_
 A condition variable to support waitForEmptyQueue(). More...
 
unsigned active_threads_
 A counter of active threads. More...
 
volatile bool stop_
 A signal to stop the threads. More...
 

Detailed Description

This class manages multiple threads and fills them with work.

Constructor & Destructor Documentation

okvis::ThreadPool::ThreadPool ( size_t  numThreads)

Constructor. Launches some amount of workers.

Parameters
[in]numThreadsThe number of threads in the pool.
okvis::ThreadPool::~ThreadPool ( )

Destructor. This joins all threads.

Member Function Documentation

template<class Function , class... Args>
std::future< typename std::result_of< Function(Args...)>::type > okvis::ThreadPool::enqueue ( Function &&  function,
Args &&...  args 
)

Enqueue work for the thread pool.

Pass in a function and its arguments to enqueue work in the thread pool

Parameters
[in]functionA function pointer to be called by a thread.
argsFunction arguments.
Returns
A std::future that will return the result of calling function. If this function is called after the thread pool has been stopped, it will return an uninitialized future that will return future.valid() == false
void okvis::ThreadPool::run ( )
private

Run a single thread.

void okvis::ThreadPool::stop ( )
inline

Stop the thread pool. This method is non-blocking.

void okvis::ThreadPool::waitForEmptyQueue ( ) const

This method blocks until the queue is empty.

Member Data Documentation

unsigned okvis::ThreadPool::active_threads_
private

A counter of active threads.

volatile bool okvis::ThreadPool::stop_
private

A signal to stop the threads.

std::queue<std::function<void()> > okvis::ThreadPool::tasks_
private

The task queue.

std::condition_variable okvis::ThreadPool::tasks_condition_
mutableprivate

A condition variable for worker threads.

std::mutex okvis::ThreadPool::tasks_mutex_
mutableprivate

A mutex to protect the list of tasks.

std::condition_variable okvis::ThreadPool::wait_condition_
mutableprivate

A condition variable to support waitForEmptyQueue().

std::vector<std::thread> okvis::ThreadPool::workers_
private

Need to keep track of threads so we can join them.


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