Robowflex  v0.1
Making MoveIt Easy
pool.cpp
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
4 
5 using namespace robowflex;
6 
7 ///
8 /// Joblet
9 ///
10 
12 {
13  canceled = true;
14 }
15 
17 {
18  return canceled;
19 }
20 
21 ///
22 /// Pool
23 ///
24 
25 Pool::Pool(unsigned int n) : active_(true)
26 {
27  for (unsigned int i = 0; i < n; ++i)
29 }
30 
32 {
33  active_ = false;
34  cv_.notify_all();
35 
36  for (auto &thread : threads_)
37  thread.join();
38 }
39 
40 unsigned int Pool::getThreadCount() const
41 {
42  return threads_.size();
43 }
44 
45 void Pool::run()
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 }
T bind(T... args)
bool isCancled() const
Checks if this job has been cancled.
Definition: pool.cpp:16
bool canceled
Whether the job is cancled or not.
Definition: pool.h:116
void cancel()
Cancels this job.
Definition: pool.cpp:11
std::mutex mutex_
Job queue mutex.
Definition: pool.h:231
unsigned int getThreadCount() const
Get the number of threads.
Definition: pool.cpp:40
bool active_
Is thread pool active?
Definition: pool.h:230
~Pool()
Destructor. Cancels all threads and joins them.
Definition: pool.cpp:31
Pool(unsigned int n=std::thread::hardware_concurrency())
Constructor.
Definition: pool.cpp:25
std::condition_variable cv_
Job queue condition variable.
Definition: pool.h:232
std::vector< std::thread > threads_
Threads.
Definition: pool.h:234
void run()
Background thread process. Executes jobs submitted from submit().
Definition: pool.cpp:45
std::queue< std::shared_ptr< Joblet > > jobs_
Jobs to execute.
Definition: pool.h:235
T emplace_back(T... args)
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
T size(T... args)