-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlockstdqueue.h
51 lines (40 loc) · 1.03 KB
/
lockstdqueue.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
// just a thread safe queue, the most simple ever
// adapted from:
// https://raw.githubusercontent.com/cameron314/concurrentqueue/master/benchmarks/stdqueue.h
// ©2014 Cameron Desrochers.
#pragma once
#include <queue>
// Simple wrapper around std::queue (not thread safe) - RC: made it thread safe
template<typename T>
class LockStdQueue
{
public:
template<typename U>
inline bool enqueue(U&& item)
{
std::lock_guard<std::mutex> guard(mutex);
q.push(std::forward<U>(item));
return true;
}
inline bool try_dequeue(T& item)
{
std::lock_guard<std::mutex> guard(mutex);
if (q.empty()) {
return false;
}
item = std::move(q.front());
q.pop();
return true;
}
unsigned long size_approx()
{
return q.size();
}
unsigned long overhead_per_element()
{
return 0; // I don't think anymore that's true. there must be some overhead
}
private:
std::queue<T> q;
mutable std::mutex mutex;
};