Helios++
Helios software for LiDAR simulations
TaskWarehouse.h
1 #pragma once
2 
3 #include <boost/thread.hpp>
4 #include <boost/thread/shared_mutex.hpp>
5 #include <boost/thread/shared_lock_guard.hpp>
6 
7 #include <vector>
8 #include <memory>
9 
10 
11 using std::vector;
12 using std::shared_ptr;
13 
21 template <typename Task>
23 protected:
24  // *** ATTRIBUTES *** //
25  // ******************** //
29  size_t maxTasks;
33  vector<shared_ptr<Task>> tasks;
37  boost::shared_mutex mtx;
41  boost::shared_mutex wmtx;
45  boost::condition_variable_any condvar;
46 
47 public:
48  // *** CONSTRUCTION / DESTRUCTION *** //
49  // ************************************ //
53  TaskWarehouse(size_t const maxTasks=256) : maxTasks(maxTasks) {}
54  virtual ~TaskWarehouse() = default;
55 
56  // *** WAREHOUSE METHODS *** //
57  // *************************** //
65  virtual bool post(shared_ptr<Task> task){
66  boost::unique_lock<boost::shared_mutex> writeLock(mtx);
67  if(tasks.size() >= maxTasks) return false;
68  tasks.push_back(task);
69  return true;
70  }
80  virtual bool post(vector<shared_ptr<Task>> &_tasks){
81  boost::unique_lock<boost::shared_mutex> writeLock(mtx);
82  if( (tasks.size()+_tasks.size()) > maxTasks) return false;
83  tasks.insert(tasks.end(), _tasks.begin(), _tasks.end());
84  return true;
85  }
86 
92  virtual shared_ptr<Task> get(){
93  boost::unique_lock<boost::shared_mutex> writeLock(mtx);
94  if(tasks.empty()) return nullptr;
95  shared_ptr<Task> task = tasks.back();
96  tasks.pop_back();
97  return task;
98  }
106  virtual vector<shared_ptr<Task>> get(size_t const n){
107  vector<shared_ptr<Task>> _tasks;
108  typename vector<shared_ptr<Task>>::iterator begin, end;
109  boost::unique_lock<boost::shared_mutex> writeLock(mtx);
110  if(tasks.empty()) return _tasks;
111  begin = tasks.end()-n;
112  end = tasks.end();
113  _tasks.insert(_tasks.end(), begin, end);
114  tasks.erase(begin, end);
115  return _tasks;
116  }
117 
121  virtual inline void wait(){
122  boost::shared_lock<boost::shared_mutex> lock(wmtx);
123  condvar.wait(lock);
124  }
130  virtual inline void waitIf(bool const &cond){
131  boost::shared_lock<boost::shared_mutex> lock(wmtx);
132  if(cond) condvar.wait(lock);
133  }
137  virtual inline void notify(){condvar.notify_one();}
141  virtual inline void notifyAll(){condvar.notify_all();}
148  virtual inline void notifyUpdate(bool &flag, bool const newValue){
149  boost::unique_lock<boost::shared_mutex> lock(wmtx);
150  flag = newValue;
151  condvar.notify_one();
152  }
159  virtual inline void notifyAllUpdate(bool &flag, bool const newValue){
160  boost::unique_lock<boost::shared_mutex> lock(wmtx);
161  flag = newValue;
162  condvar.notify_all();
163  }
164 
165  // *** GETTERs and SETTERs *** //
166  // ***************************** //
173  boost::shared_lock<boost::shared_mutex> lock(mtx);
174  return !tasks.empty();
175  }
176 };
Class implementing a warehouse to store and retrieve tasks in a thread safe fashion.
Definition: TaskWarehouse.h:22
virtual vector< shared_ptr< Task > > get(size_t const n)
Get tasks from warehouse, if any.
Definition: TaskWarehouse.h:106
bool hasPendingTasks()
Check if there are pending tasks in the warehouse (true) or not (false)
Definition: TaskWarehouse.h:172
vector< shared_ptr< Task > > tasks
Tasks handled by the warehouse.
Definition: TaskWarehouse.h:33
virtual void notifyAllUpdate(bool &flag, bool const newValue)
Notify all waiting threads after updating flag with new value in a thread-safe way.
Definition: TaskWarehouse.h:159
size_t maxTasks
Maximum number of tasks the warehouse can handle.
Definition: TaskWarehouse.h:29
boost::shared_mutex wmtx
The wait mutex to implement non-active listening waiting.
Definition: TaskWarehouse.h:41
virtual shared_ptr< Task > get()
Get task from warehouse, if any. Retrieved task is removed from warehouse.
Definition: TaskWarehouse.h:92
virtual void notify()
Notify a waiting thread to stop waiting.
Definition: TaskWarehouse.h:137
virtual bool post(shared_ptr< Task > task)
Post task into warehouse. Posted task will be stored in the warehouse only if there is enough availab...
Definition: TaskWarehouse.h:65
virtual void notifyAll()
Notify all waiting threads to stop waiting.
Definition: TaskWarehouse.h:141
virtual bool post(vector< shared_ptr< Task >> &_tasks)
Post tasks into warehouse. Posted tasks will be stored in the warehouse only if there is enough avail...
Definition: TaskWarehouse.h:80
virtual void wait()
Caller thread waits until notified.
Definition: TaskWarehouse.h:121
TaskWarehouse(size_t const maxTasks=256)
Default constructor for task warehouse.
Definition: TaskWarehouse.h:53
virtual void waitIf(bool const &cond)
Caller thread waits until notified but only if condition is satisfied (true)
Definition: TaskWarehouse.h:130
boost::shared_mutex mtx
The mutex to handle concurrent access to the tasks.
Definition: TaskWarehouse.h:37
virtual void notifyUpdate(bool &flag, bool const newValue)
Notify a waiting thread after updating flag with new value in a thread-safe way.
Definition: TaskWarehouse.h:148
boost::condition_variable_any condvar
Conditional variable to handle wait/notify signals.
Definition: TaskWarehouse.h:45