-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent_queue.h
92 lines (76 loc) · 1.87 KB
/
concurrent_queue.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Created by jackson on 15.04.16.
//
#ifndef KIMBERLY_COCURRENT_QUEUE_H
#define KIMBERLY_COCURRENT_QUEUE_H
#include <memory>
#include <chrono>
#include <cassert>
#include <iostream>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include "util.h"
template<typename T>
class concurrent_queue {
public:
concurrent_queue() { }
concurrent_queue(const concurrent_queue &) = delete;
concurrent_queue &operator=(const concurrent_queue &) = delete;
bool peek(T &res) {
std::lock_guard<std::mutex> lock(mutex);
if (queue.empty()) {
return false;
}
Log::d("pp3");
res = queue.front();
Log::d("pp4");
queue.pop();
Log::d("pp5");
return true;
}
//TODO: move
bool pop(T &res) {
Log::d("popping");
std::unique_lock<std::mutex> lock(mutex);
if (releasing) {
return false;
}
Log::d("cp1");
cond.wait(lock, [this] { return !queue.empty() || releasing; });
if (releasing) {
return false;
}
Log::d("cp2");
res = queue.front();
Log::d("cp3");
queue.pop();
Log::d("cp4");
return true;
}
void push(const T &item) {
std::lock_guard<std::mutex> lock(mutex);
queue.push(item);
cond.notify_one();
}
void push(const T &&item) {
std::lock_guard<std::mutex> lock(mutex);
queue.push(std::move(item));
cond.notify_one();
}
void release_waiters() {
std::lock_guard<std::mutex> lock(mutex);
releasing = true;
cond.notify_all();
}
bool is_releasing() {
return releasing;
}
private:
std::queue<T> queue;
std::mutex mutex;
std::condition_variable cond;
bool releasing = false;
};
#endif //KIMBERLY_COCURRENT_QUEUE_H