-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_queue.h
54 lines (44 loc) · 1.13 KB
/
my_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
#ifndef THERMAL_CONDUCTIVITY_2D_MY_QUEUE_H
#define THERMAL_CONDUCTIVITY_2D_MY_QUEUE_H
#include <iostream>
#include <mutex>
#include <queue>
template<typename T>
class MyQueue{
private:
std::mutex mtx;
std::condition_variable cv;
std::queue<T> *q = new std::queue<T>;
bool notified = false;
bool finished = false;
public:
void finish(){
std::lock_guard<std::mutex> lock(mtx);
finished = true;
notified = true;
cv.notify_all();
}
void push(T element){
std::unique_lock<std::mutex> lock(mtx);
q->push(element);
notified = true;
cv.notify_one();
}
std::vector<T> pop(){
std::vector<T> some;
std::unique_lock<std::mutex> lock(mtx);
while (!finished || (q->size() >= 1)) {
while(!notified && q->empty()){
cv.wait(lock);
}
while(q->size() >= 1) {
some.emplace_back(q->front());
q->pop();
return some;
}
notified = false;
}
return some;
}
};
#endif //THERMAL_CONDUCTIVITY_2D_MY_QUEUE_H