Helios++
Helios software for LiDAR simulations
SimpleThreadPool.h
1 #pragma once
2 
3 #include <ThreadPool.h>
4 
11 template <typename ... TaskArgs>
13 protected:
16  // *** ATTRIBUTES *** //
17  // ******************** //
22  std::size_t available_;
26  boost::mutex mutex_;
31  boost::condition_variable cond_;
32 
33 public:
34  // *** CONSTRUCTION / DESTRUCTION *** //
35  // ************************************ //
41  explicit SimpleThreadPool(std::size_t const _pool_size) :
42  ThreadPool(_pool_size),
43  available_(_pool_size)
44  {}
45  virtual ~SimpleThreadPool() = default;
46 
47  // *** M E T H O D S *** //
48  // *********************** //
52  template <typename Task>
53  void run_task(Task task){
54  boost::unique_lock<boost::mutex> lock(mutex_);
55 
56  // If no threads are available, then wait for a thread to finish.
57  if (0 == available_){
58  cond_.wait(lock);
59  }
60 
61  // Decrement count, indicating thread is no longer available.
62  --available_;
63 
64  // Unlock the mutex
65  lock.unlock();
66 
67  // Post a wrapped task into the queue
68  io_service_.post(
69  boost::bind(
71  this,
72  boost::function<void(TaskArgs ...)>(task)
73  )
74  );
75  }
76 
80  virtual void join(){
81  boost::unique_lock<boost::mutex> lock(mutex_);
82  while(available_ < pool_size){
83  cond_.wait(lock);
84  }
85  }
86 
87 protected:
93  virtual void wrap_task(
94  boost::function<void(TaskArgs ...)> &task
95  ){
96  // Run the user supplied task.
97  try{
98  do_task(task);
99  }
100  // Suppress all exceptions.
101  catch (const std::exception &e) {
102  std::stringstream ss;
103  ss << "ThreadPool::wrap_task EXCEPTION: " << e.what();
104  logging::WARN(ss.str());
105  }
106 
107  // Task has finished, so increment count of available threads.
108  boost::unique_lock<boost::mutex> lock(mutex_);
109  ++available_;
110  lock.unlock();
111  cond_.notify_one();
112  }
113 
118  virtual void do_task(
119  boost::function<void(TaskArgs ...)> &task
120  ) = 0;
121 
122 public:
123  // *** EXTERNAL HANDLING *** //
124  // *************************** //
129  virtual inline void notifyOne(){cond_.notify_one();}
130 };
Abstract class providing implementation of a simple thread pool which assigns tasks to threads.
Definition: SimpleThreadPool.h:12
std::size_t pool_size
Size of thread pool (number of threads)
Definition: ThreadPool.h:47
boost::mutex mutex_
Mutex to handle concurrent tasks.
Definition: SimpleThreadPool.h:26
virtual void do_task(boost::function< void(TaskArgs ...)> &task)=0
Invoke task with corresponding arguments.
boost::asio::io_service io_service_
Instance of boost input/output service for asynchronous data processing.
Definition: ThreadPool.h:36
boost::condition_variable cond_
Condition variable to handle tasks dispatching depending on available threads.
Definition: SimpleThreadPool.h:31
SimpleThreadPool(std::size_t const _pool_size)
Simple thread pool constructor.
Definition: SimpleThreadPool.h:41
virtual void join()
Lock until all pending threads have finished.
Definition: SimpleThreadPool.h:80
virtual void wrap_task(boost::function< void(TaskArgs ...)> &task)
Wrap a task so that available threads count can be increased once provided task has been completed.
Definition: SimpleThreadPool.h:93
virtual void notifyOne()
Notify the conditional variable so if there is a waiting thread it will wake up.
Definition: SimpleThreadPool.h:129
std::size_t available_
Number of available threads, those which are not currently performing a task.
Definition: SimpleThreadPool.h:22
void run_task(Task task)
Run a task when there is an available thread for it.
Definition: SimpleThreadPool.h:53
Base class providing core implementation of a thread pool to deal with multi threading tasks.
Definition: ThreadPool.h:28
std::size_t pool_size
Size of thread pool (number of threads)
Definition: ThreadPool.h:47
boost::asio::io_service io_service_
Instance of boost input/output service for asynchronous data processing.
Definition: ThreadPool.h:36