-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.h
More file actions
38 lines (32 loc) · 699 Bytes
/
queue.h
File metadata and controls
38 lines (32 loc) · 699 Bytes
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
#pragma once
#include <queue>
#include <mutex>
#include <condition_variable>
template <typename T>
class ThreadQueue {
public:
ThreadQueue() {
}
void Push(const T& t) {
{
std::lock_guard<std::mutex> guard(m_);
q_.push(t);
}
cond_var_.notify_one();
}
T Pop() {
{
std::unique_lock<std::mutex> lock(m_);
if (q_.empty()) {
cond_var_.wait(lock, [this] { return !q_.empty(); });
}
T res = q_.front();
q_.pop();
return res;
}
}
private:
std::queue<T> q_;
std::mutex m_;
std::condition_variable cond_var_;
};