SurgSim::Framework::ThreadPool Class Reference

A thread pool for completing heterogenous tasks. More...

`#include <SurgSim/Framework/ThreadPool.h>`

## Classes

class | Task
| Actual tasks, with typed return type. More...
class | TaskBase

## Public Member Functions

| ThreadPool (size_t numThreads=boost::thread::hardware_concurrency())
| Constructor. More...
| ~ThreadPool ()
| Desctructor. More...
template<class R >
std::future< R > | enqueue (std::function< R()> function)
| Queue a task to be run by the ThreadPool. More...

## Private Member Functions

| ThreadPool (const ThreadPool &other)
ThreadPool & | operator= (const ThreadPool &other)

## Private Attributes

std::list< boost::thread > | m_threads
| The worker threads. More...
std::queue< std::unique_ptr< TaskBase > > | m_tasks
| Queued tasks waiting for an available thread. More...
boost::mutex | m_mutex
| Mutex for protecting the tasks queue. More...
boost::condition_variable | m_threadSignaler
| Signaler for waking up threads waiting for tasks. More...
std::atomic< bool > | m_destructing
| True if the ThreadPool is destructing. More...

## Detailed Description

A thread pool for completing heterogenous tasks.

The thread pool is a class that completes given tasks using a set of worker threads. These threads pull tasks off of a task queue. Once finished with the task, the thread gets the next task if one is available, or waits for another task to be added. The tasks can be heterogenous, meaning any callable target can be added with any return type.

Example Usage:

double f1() { return 1.0; }
int f2(int val) { return val; }
int main()
{
ThreadPool pool;
// Add a task
std::future<double> result1 = pool.enqueue<double>(f1);
// Add a task using std::bind
std::future<int> result2 = pool.enqueue<int>(std::bind(f2, 2));
// Add a task using a lambda function
std::future<std::string> result3 = pool.enqueue<std::string>([]() {return "string"; });
// Print out result when task is completed
std::cout << "Result 1: " << result1.get() << std::endl;
std::cout << "Result 2: " << result2.get() << std::endl;
std::cout << "Result 3: " << result3.get() << std::endl;
}

## Constructor & Destructor Documentation

## ThreadPool() [1/2]

SurgSim::Framework::ThreadPool::ThreadPool | ( | size_t | numThreads = `boost::thread::hardware_concurrency()` | ) |

explicit

Constructor.

Parameters

  numThreads | The number of worker threads

## ~ThreadPool()

SurgSim::Framework::ThreadPool::~ThreadPool | ( |  | ) |

Desctructor.

## ThreadPool() [2/2]

SurgSim::Framework::ThreadPool::ThreadPool | ( | const ThreadPool & | other | ) |

private

Prevent default copy construction and default assignment

## Member Function Documentation

## enqueue()

template<class R >

std::future< R > SurgSim::Framework::ThreadPool::enqueue | ( | std::function< R()> | function | ) |

Queue a task to be run by the ThreadPool.

Note
  The task must not take any arguments. To add a function that does require arguments use std::bind.

Template Parameters

  R | return type of the task

Parameters

  function | The task to be queued

Returns
  a std::future that holds the results (of type R) once completed

## operator=()

ThreadPool& SurgSim::Framework::ThreadPool::operator= | ( | const ThreadPool & | other | ) |

private

Prevent default copy construction and default assignment

## Member Data Documentation

## m_destructing

std::atomic<bool> SurgSim::Framework::ThreadPool::m_destructing

private

True if the ThreadPool is destructing.

## m_mutex

boost::mutex SurgSim::Framework::ThreadPool::m_mutex

private

Mutex for protecting the tasks queue.

## m_tasks

std::queue<std::unique_ptr<TaskBase> > SurgSim::Framework::ThreadPool::m_tasks

private

Queued tasks waiting for an available thread.

## m_threads

std::list<boost::thread> SurgSim::Framework::ThreadPool::m_threads

private

The worker threads.

## m_threadSignaler

boost::condition_variable SurgSim::Framework::ThreadPool::m_threadSignaler

private

Signaler for waking up threads waiting for tasks.

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

- SurgSim/Framework/ThreadPool.h
- SurgSim/Framework/ThreadPool-inl.h
- SurgSim/Framework/ThreadPool.cpp