3 #include <boost/thread.hpp>
4 #include <boost/thread/shared_mutex.hpp>
5 #include <boost/thread/shared_lock_guard.hpp>
12 using std::shared_ptr;
21 template <
typename Task>
37 boost::shared_mutex
mtx;
65 virtual bool post(shared_ptr<Task> task){
66 boost::unique_lock<boost::shared_mutex> writeLock(
mtx);
68 tasks.push_back(task);
80 virtual bool post(vector<shared_ptr<Task>> &_tasks){
81 boost::unique_lock<boost::shared_mutex> writeLock(
mtx);
83 tasks.insert(
tasks.end(), _tasks.begin(), _tasks.end());
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();
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;
113 _tasks.insert(_tasks.end(), begin, end);
114 tasks.erase(begin, end);
122 boost::shared_lock<boost::shared_mutex> lock(
wmtx);
130 virtual inline void waitIf(
bool const &cond){
131 boost::shared_lock<boost::shared_mutex> lock(
wmtx);
149 boost::unique_lock<boost::shared_mutex> lock(
wmtx);
160 boost::unique_lock<boost::shared_mutex> lock(
wmtx);
173 boost::shared_lock<boost::shared_mutex> lock(
mtx);
174 return !
tasks.empty();
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