-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTaskQueue.h
63 lines (53 loc) · 1.15 KB
/
TaskQueue.h
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// Created by asalehin on 7/18/20.
//
#ifndef MODULE_NAME
#define MODULE_NAME "ThreadPool"
#endif
#include <queue>
#include <atomic>
#include <thread>
#include <functional>
#include <chrono>
using namespace std;
class TaskQueue {
public:
TaskQueue() {
deque<function<void()>> qu;
q = make_unique<deque<function<void()>>>(move(qu));
start_queue();
}
void stop_queue() {
is_running = false;
}
void enqueue(function<void()>&& f) {
q->push_back(move(f));
}
void clear_queue() {
if (q) {
q->clear();
}
}
private:
const char* TAG = "TaskQueue:: %d";
unique_ptr<deque<function<void()>>> q;
atomic<bool> is_running;
thread t;
void start_queue() {
is_running = true;
t = thread([this] {
this->executor_loop();
});
t.detach();
}
void executor_loop() {
while (is_running) {
if (!q->empty()) {
auto f = q->front();
f();
q->pop_front();
this_thread::sleep_for(chrono::microseconds (1000));
}
}
}
};