-
Notifications
You must be signed in to change notification settings - Fork 0
/
queued_executor.cpp
38 lines (35 loc) · 990 Bytes
/
queued_executor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "queued_executor.hpp"
#include <iostream>
QueuedExecutor::QueuedExecutor() :
mDoRun(true),
mThread([this](){run();}) {
}
void QueuedExecutor::submit(const std::shared_ptr<ITask> &task) {
std::lock_guard guard(mMutex);
mQueue.push(task);
}
void QueuedExecutor::run() {
while(mDoRun) {
std::shared_ptr<ITask> task;
{
std::lock_guard lock(mMutex);
if(mQueue.size() > 0) {
task = mQueue.front();
mQueue.pop();
}
}
if(task) {
auto executionResult = TaskExecutionResult::NotExecuted;
try {
executionResult = task->execute();
} catch(...) {
std::cerr << "Error during task execution" << std::endl;
}
if(executionResult == TaskExecutionResult::Resubmit) {
submit(task);
}
} else {
std::this_thread::yield();
}
}
}