Ocean
TaskQueue.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_TASK_QUEUE_H
9 #define META_OCEAN_BASE_TASK_QUEUE_H
10 
11 #include "ocean/base/Base.h"
12 #include "ocean/base/Caller.h"
13 #include "ocean/base/Singleton.h"
14 #include "ocean/base/Thread.h"
15 
16 #include <queue>
17 
18 namespace Ocean
19 {
20 
21 /**
22  * This class implements a queue for tasks.
23  * The queue is based on a single thread so that all tasks will be processes sequential in a normal FIFO queue order.<br>
24  * In contrast to the Scheduler the TaskQueue invokes a function only once.<br>
25  * @see Scheduler, ThreadPool.
26  * @ingroup base
27  */
28 class OCEAN_BASE_EXPORT TaskQueue :
29  public Singleton<TaskQueue>,
30  protected Thread
31 {
32  friend class Singleton<TaskQueue>;
33 
34  public:
35 
36  /**
37  * Definition of a task as caller function.
38  */
39  typedef Caller<void> Task;
40 
41  protected:
42 
43  /**
44  * Definition of a queue holding tasks.
45  */
46  typedef std::queue<Task> Tasks;
47 
48  public:
49 
50  /**
51  * Adds a new tasks to the queue which will be invoked after all previous task have been invoked.
52  * @param task The new task to add
53  */
54  void pushTask(const Task& task);
55 
56  /**
57  * Returns the number of tasks currently in the queue (not counting an currently invoked task).
58  * @return The number of pending tasks.
59  */
60  size_t pendingTasks();
61 
62  /**
63  * Removes all pending tasks from this queue.
64  * A currently invoked task will not be stopped.
65  */
66  void clear();
67 
68  protected:
69 
70  /**
71  * Creates a new task queue object.
72  */
74 
75  /**
76  * Destructs a task queue object.
77  */
79 
80  /**
81  * The thread run function.
82  */
83  virtual void threadRun();
84 
85  protected:
86 
87  /// The tasks if this queue.
89 
90  /// True, if at least one task has been added to this queue before.
92 
93  /// The lock of this queue.
95 };
96 
97 }
98 
99 #endif // META_OCEAN_BASE_TASK_QUEUE_H
This class implements a recursive lock object.
Definition: Lock.h:31
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
This class implements a queue for tasks.
Definition: TaskQueue.h:31
virtual void threadRun()
The thread run function.
size_t pendingTasks()
Returns the number of tasks currently in the queue (not counting an currently invoked task).
Tasks queueTasks
The tasks if this queue.
Definition: TaskQueue.h:88
Lock queueLock
The lock of this queue.
Definition: TaskQueue.h:94
void pushTask(const Task &task)
Adds a new tasks to the queue which will be invoked after all previous task have been invoked.
bool queueHadTask
True, if at least one task has been added to this queue before.
Definition: TaskQueue.h:91
std::queue< Task > Tasks
Definition of a queue holding tasks.
Definition: TaskQueue.h:46
Caller< void > Task
Definition of a task as caller function.
Definition: TaskQueue.h:39
void clear()
Removes all pending tasks from this queue.
TaskQueue()
Creates a new task queue object.
~TaskQueue()
Destructs a task queue object.
This class implements a thread.
Definition: Thread.h:115
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15