23 #include <condition_variable> 44 :m_processTaskFunc(processTaskFunc)
47 m_runWorkerThread =
true;
48 m_workerThread = std::thread(&TaskQueue::worker,
this);
57 std::unique_lock<std::mutex> lck(m_taskQueueMutex);
58 m_runWorkerThread =
false;
61 m_conditionVariable.notify_all();
63 if (m_workerThread.joinable())
64 m_workerThread.join();
77 std::unique_lock<std::mutex> lck(m_taskQueueMutex);
78 m_taskQueue.push(task);
79 retval = m_taskQueue.size();
82 m_conditionVariable.notify_all();
92 std::unique_lock<std::mutex> lck(m_taskQueueMutex);
93 m_runWorkerThread =
false;
96 m_conditionVariable.notify_all();
105 std::unique_lock<std::mutex> lck(m_taskQueueMutex);
106 retval = m_taskQueue.size();
115 std::unique_lock<std::mutex> lck(m_taskQueueMutex, std::defer_lock);
117 while (m_runWorkerThread) {
121 m_conditionVariable.wait(lck, [&] {
return m_taskPushed; });
123 m_taskPushed =
false;
125 while (m_runWorkerThread) {
126 if (!m_taskQueue.empty()) {
127 auto task = m_taskQueue.front();
130 m_processTaskFunc(task);
141 std::mutex m_taskQueueMutex;
142 std::condition_variable m_conditionVariable;
143 std::queue<T> m_taskQueue;
145 bool m_runWorkerThread;
146 std::thread m_workerThread;
148 ProcessTaskFunc m_processTaskFunc;
int pushToQueue(const T &task)
Push task to queue.
Definition: TaskQueue.h:73
TaskQueue(ProcessTaskFunc processTaskFunc)
constructor
Definition: TaskQueue.h:43
virtual ~TaskQueue()
destructor
Definition: TaskQueue.h:54
size_t size()
Get actual queue size.
Definition: TaskQueue.h:101
void stopQueue()
Stop queue.
Definition: TaskQueue.h:89
std::function< void(T)> ProcessTaskFunc
Processing function type.
Definition: TaskQueue.h:36
Maintain queue of tasks and invoke sequential processing.
Definition: TaskQueue.h:32