TaskQueue.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_THREADPOOL_TASKQUEUE_H_
36 #define _BLAZE_UTIL_THREADPOOL_TASKQUEUE_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <deque>
45 
46 
47 namespace blaze {
48 
49 namespace threadpool {
50 
51 //=================================================================================================
52 //
53 // CLASS DEFINITION
54 //
55 //=================================================================================================
56 
57 //*************************************************************************************************
64 class TaskQueue
65 {
66  private:
67  //**Type definitions****************************************************************************
68  using Tasks = std::deque<Task>;
69  //**********************************************************************************************
70 
71  public:
72  //**Type definitions****************************************************************************
73  using SizeType = Tasks::size_type;
74  //**********************************************************************************************
75 
76  //**Constructor*********************************************************************************
79  explicit inline TaskQueue();
81  //**********************************************************************************************
82 
83  //**Destructor**********************************************************************************
86  inline ~TaskQueue();
88  //**********************************************************************************************
89 
90  //**Get functions*******************************************************************************
93  inline SizeType maxSize() const;
94  inline SizeType size() const;
95  inline bool isEmpty() const;
97  //**********************************************************************************************
98 
99  //**Element functions***************************************************************************
102  inline void push ( Task task );
103  inline Task pop ();
104  inline void clear();
106  //**********************************************************************************************
107 
108  //**Utility functions***************************************************************************
111  inline void swap( TaskQueue& tq ) noexcept;
113  //**********************************************************************************************
114 
115  private:
116  //**Member variables****************************************************************************
120 
121  //**********************************************************************************************
122 };
123 //*************************************************************************************************
124 
125 
126 
127 
128 //=================================================================================================
129 //
130 // CONSTRUCTOR
131 //
132 //=================================================================================================
133 
134 //*************************************************************************************************
138  : tasks_() // FIFO container for the contained tasks
139 {}
140 //*************************************************************************************************
141 
142 
143 
144 
145 //=================================================================================================
146 //
147 // DESTRUCTOR
148 //
149 //=================================================================================================
150 
151 //*************************************************************************************************
157 {
158  clear();
159 }
160 //*************************************************************************************************
161 
162 
163 
164 
165 //=================================================================================================
166 //
167 // GET FUNCTIONS
168 //
169 //=================================================================================================
170 
171 //*************************************************************************************************
177 {
178  return tasks_.max_size();
179 }
180 //*************************************************************************************************
181 
182 
183 //*************************************************************************************************
191 {
192  return tasks_.size();
193 }
194 //*************************************************************************************************
195 
196 
197 //*************************************************************************************************
202 inline bool TaskQueue::isEmpty() const
203 {
204  return tasks_.empty();
205 }
206 //*************************************************************************************************
207 
208 
209 
210 
211 //=================================================================================================
212 //
213 // ELEMENT FUNCTIONS
214 //
215 //=================================================================================================
216 
217 //*************************************************************************************************
225 inline void TaskQueue::push( Task task )
226 {
227  tasks_.push_back( task );
228 }
229 //*************************************************************************************************
230 
231 
232 //*************************************************************************************************
238 {
239  const Task task( tasks_.front() );
240  tasks_.pop_front();
241  return task;
242 }
243 //*************************************************************************************************
244 
245 
246 //*************************************************************************************************
251 inline void TaskQueue::clear()
252 {
253  tasks_.clear();
254 }
255 //*************************************************************************************************
256 
257 
258 
259 
260 //=================================================================================================
261 //
262 // UTILITY FUNCTIONS
263 //
264 //=================================================================================================
265 
266 //*************************************************************************************************
273 inline void TaskQueue::swap( TaskQueue& tq ) noexcept
274 {
275  tasks_.swap( tq.tasks_ );
276 }
277 //*************************************************************************************************
278 
279 
280 
281 
282 //=================================================================================================
283 //
284 // GLOBAL OPERATORS
285 //
286 //=================================================================================================
287 
288 //*************************************************************************************************
291 inline void swap( TaskQueue& a, TaskQueue& b ) noexcept;
293 //*************************************************************************************************
294 
295 
296 //*************************************************************************************************
304 inline void swap( TaskQueue& a, TaskQueue& b ) noexcept
305 {
306  a.swap( b );
307 }
308 //*************************************************************************************************
309 
310 } // namespace threadpool
311 
312 } // namespace blaze
313 
314 
315 #endif
SizeType maxSize() const
Returns the maximum possible size of a task queue.
Definition: TaskQueue.h:176
bool isEmpty() const
Returns true if the task queue has no elements.
Definition: TaskQueue.h:202
Task pop()
Returns the task from the front of the task queue.
Definition: TaskQueue.h:237
void swap(TaskQueue &tq) noexcept
Swapping the contents of two task queues.
Definition: TaskQueue.h:273
std::function< void(void)> Task
Handle for a single, executable task.
Definition: Task.h:60
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Tasks tasks_
FIFO container for the contained tasks.
Definition: TaskQueue.h:119
TaskQueue()
Default constructor for TaskQueue.
Definition: TaskQueue.h:137
std::deque< Task > Tasks
FIFO container for tasks.
Definition: TaskQueue.h:68
~TaskQueue()
Destructor for the TaskQueue class.
Definition: TaskQueue.h:156
Header file for the Task base class.
void push(Task task)
Adding a task to the end of the task queue.
Definition: TaskQueue.h:225
void swap(TaskQueue &a, TaskQueue &b) noexcept
Swapping the contents of two task queues.
Definition: TaskQueue.h:304
Task queue for the thread pool.The TaskQueue class represents the internal task container of a thread...
Definition: TaskQueue.h:64
void clear()
Removing all tasks from the task queue.
Definition: TaskQueue.h:251
SizeType size() const
Returns the current size of the task queue.
Definition: TaskQueue.h:190
Tasks::size_type SizeType
Size type of the task queue.
Definition: TaskQueue.h:73