-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcurrentQueue.hpp
53 lines (42 loc) · 1.01 KB
/
ConcurrentQueue.hpp
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
//
// Created by boy on 05/07/22.
//
#ifndef DAG_LIB_CONCURRENTQUEUE_HPP
#define DAG_LIB_CONCURRENTQUEUE_HPP
#include <condition_variable>
#include <functional>
#include <mutex>
#include <queue>
#include "Utils.hpp"
using namespace std;
class ConcurrentQueue {
private:
queue<function<void()>> m_queue;
mutex m_mutex;
public:
ConcurrentQueue() = default;
bool empty() {
unique_lock<mutex> lock(m_mutex);
return m_queue.empty();
}
int size() {
unique_lock<mutex> lock(m_mutex);
return m_queue.size();
}
void push(function<void()> t) {
std::unique_lock<std::mutex> lock(m_mutex);
m_queue.push(t);
}
bool pop(function<void()>& t) {
std::unique_lock<std::mutex> lock(m_mutex);
if (m_queue.empty()) {
return false;
}
t = move(m_queue.front());
m_queue.pop();
// STOP(time, elapsed);
// cout << elapsed<<endl;
return true;
}
};
#endif //DAG_LIB_CONCURRENTQUEUE_HPP