![]() |
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... | |
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:
|
explicit |
Constructor.
numThreads | The number of worker threads |
SurgSim::Framework::ThreadPool::~ThreadPool | ( | ) |
Desctructor.
|
private |
Prevent default copy construction and default assignment
std::future< R > SurgSim::Framework::ThreadPool::enqueue | ( | std::function< R()> | function | ) |
Queue a task to be run by the ThreadPool.
R | return type of the task |
function | The task to be queued |
|
private |
Prevent default copy construction and default assignment
|
private |
True if the ThreadPool is destructing.
|
private |
Mutex for protecting the tasks queue.
|
private |
Queued tasks waiting for an available thread.
|
private |
The worker threads.
|
private |
Signaler for waking up threads waiting for tasks.