-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathblocking_queue.h
57 lines (51 loc) · 953 Bytes
/
blocking_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
#ifndef CPROVER_UTIL_BLOCKING_QUEUE
#define CPROVER_UTIL_BLOCKING_QUEUE
#include <mutex>
#include <condition_variable>
#include <queue>
template <typename T> class blocking_queue {
std::condition_variable can_pop;
std::mutex sync;
std::queue<T> qu;
bool shutdown = false;
public:
void push(const T& item)
{
{
std::unique_lock<std::mutex> lock(sync);
qu.push(item);
}
can_pop.notify_one();
}
void request_shutdown()
{
{
std::unique_lock<std::mutex> lock(sync);
shutdown = true;
}
can_pop.notify_all();
}
bool pop(T &item)
{
std::unique_lock<std::mutex> lock(sync);
for (;;)
{
if (qu.empty())
{
if (shutdown)
{
return false;
}
}
else
{
break;
}
can_pop.wait(lock);
}
item = std::move(qu.front());
qu.pop();
return true;
}
};
#endif // CPROVER_UTIL_BLOCKING_QUEUE