Robowflex  v0.1
Making MoveIt Easy
robowflex::Pool Class Reference

A thread pool that can execute arbitrary functions asynchronously. Functions with arguments to be executed are put in the queue through submit(). This returns a Pool::Job that can be used to retrieve the result or cancel the job if the result is no longer needed. More...

#include <pool.h>

Classes

class  Job
 A job that returns RT. More...
 
class  Joblet
 Interface class for Pool::Job so template parameters are not needed for the queue. More...
 

Public Member Functions

 Pool (unsigned int n=std::thread::hardware_concurrency())
 Constructor. More...
 
 ~Pool ()
 Destructor. Cancels all threads and joins them. More...
 
unsigned int getThreadCount () const
 Get the number of threads. More...
 
template<typename RT , typename... Args>
std::shared_ptr< Job< RT > > submit (const std::function< RT(Args...)> &&function, Args &&... args) const
 Submit a function with arguments to be processed by the thread pool. Submitted functions must be wrapped with robowflex::make_function() or be a std::function type so argument template deduction works. More...
 
void run ()
 Background thread process. Executes jobs submitted from submit(). More...
 

Private Attributes

bool active_ {false}
 Is thread pool active? More...
 
std::mutex mutex_
 Job queue mutex. More...
 
std::condition_variable cv_
 Job queue condition variable. More...
 
std::vector< std::threadthreads_
 Threads. More...
 
std::queue< std::shared_ptr< Joblet > > jobs_
 Jobs to execute. More...
 

Detailed Description

A thread pool that can execute arbitrary functions asynchronously. Functions with arguments to be executed are put in the queue through submit(). This returns a Pool::Job that can be used to retrieve the result or cancel the job if the result is no longer needed.

make_function implementation taken from: https://stackoverflow.com/questions/27825559/why-is-there-no-stdmake-function/27826081#27826081 TODO: Investigate other APIs for the submit() function in robowflex::Pool.

Definition at line 94 of file pool.h.

Constructor & Destructor Documentation

◆ Pool()

Pool::Pool ( unsigned int  n = std::thread::hardware_concurrency())

Constructor.

Parameters
[in]nThe number of threads to use. By default uses available hardware threads.

Pool

Definition at line 25 of file pool.cpp.

25  : active_(true)
26 {
27  for (unsigned int i = 0; i < n; ++i)
29 }
T bind(T... args)
bool active_
Is thread pool active?
Definition: pool.h:230
std::vector< std::thread > threads_
Threads.
Definition: pool.h:234
void run()
Background thread process. Executes jobs submitted from submit().
Definition: pool.cpp:45
T emplace_back(T... args)

◆ ~Pool()

Pool::~Pool ( )

Destructor. Cancels all threads and joins them.

Definition at line 31 of file pool.cpp.

32 {
33  active_ = false;
34  cv_.notify_all();
35 
36  for (auto &thread : threads_)
37  thread.join();
38 }
std::condition_variable cv_
Job queue condition variable.
Definition: pool.h:232

Member Function Documentation

◆ getThreadCount()

unsigned int Pool::getThreadCount ( ) const

Get the number of threads.

Returns
The number of threads.

Definition at line 40 of file pool.cpp.

41 {
42  return threads_.size();
43 }
T size(T... args)

◆ run()

void Pool::run ( )

Background thread process. Executes jobs submitted from submit().

Definition at line 45 of file pool.cpp.

46 {
47  while (active_)
48  {
50  cv_.wait(lock, [&] { return (active_ && !jobs_.empty()) || !active_; });
51 
52  if (!active_)
53  break;
54 
55  auto job = jobs_.front();
56  jobs_.pop();
57 
58  lock.unlock();
59 
60  // Ignore canceled jobs.
61  if (!job->isCancled())
62  job->execute();
63  }
64 }
std::mutex mutex_
Job queue mutex.
Definition: pool.h:231
std::queue< std::shared_ptr< Joblet > > jobs_
Jobs to execute.
Definition: pool.h:235
T lock(T... args)

◆ submit()

template<typename RT , typename... Args>
std::shared_ptr<Job<RT> > robowflex::Pool::submit ( const std::function< RT(Args...)> &&  function,
Args &&...  args 
) const
inline

Submit a function with arguments to be processed by the thread pool. Submitted functions must be wrapped with robowflex::make_function() or be a std::function type so argument template deduction works.

Parameters
[in]functionFunction to execute.
[in]argsArguments to the function.
Template Parameters
RTReturn type of function.
ArgsTypes of the arguments to the function.
Returns
A job that contains information about the submitted function. This job can be canceled, which results in no execution of the function by the queue.

Definition at line 209 of file pool.h.

210  {
211  auto job = std::make_shared<Job<RT>>(std::forward<const std::function<RT(Args...)>>(function),
212  std::forward<Args>(args)...);
213 
214  {
216  jobs_.emplace(job);
217 
218  cv_.notify_one();
219  }
220 
221  return job;
222  }
T forward(T... args)

Member Data Documentation

◆ active_

bool robowflex::Pool::active_ {false}
private

Is thread pool active?

Definition at line 230 of file pool.h.

◆ cv_

std::condition_variable robowflex::Pool::cv_
mutableprivate

Job queue condition variable.

Definition at line 232 of file pool.h.

◆ jobs_

std::queue<std::shared_ptr<Joblet> > robowflex::Pool::jobs_
mutableprivate

Jobs to execute.

Definition at line 235 of file pool.h.

◆ mutex_

std::mutex robowflex::Pool::mutex_
mutableprivate

Job queue mutex.

Definition at line 231 of file pool.h.

◆ threads_

std::vector<std::thread> robowflex::Pool::threads_
private

Threads.

Definition at line 234 of file pool.h.


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