Debian Codegraph @

thirdparty/lockstdqueue.h

raw
1 // just a thread safe queue, the most simple ever
2
3 // adapted from:
4 // https://raw.githubusercontent.com/cameron314/concurrentqueue/master/benchmarks/stdqueue.h
5 // ©2014 Cameron Desrochers.
6
7 #pragma once
8
9 #include <queue>
10
11
12 // Simple wrapper around std::queue (not thread safe) - RC: made it thread safe
13 template<typename T>
14 class LockStdQueue
15 {
16
17 public:
18 template<typename U>
19 inline bool enqueue(U&& item)
20 {
21 std::lock_guard<std::mutex> guard(mutex);
22 q.push(std::forward<U>(item));
23 return true;
24 }
25
26 inline bool try_dequeue(T& item)
27 {
28 std::lock_guard<std::mutex> guard(mutex);
29 if (q.empty()) {
30 return false;
31 }
32
33 item = std::move(q.front());
34 q.pop();
35 return true;
36 }
37
38 unsigned long size_approx()
39 {
40 return q.size();
41 }
42
43 unsigned long overhead_per_element()
44 {
45 return 0; // I don't think anymore that's true. there must be some overhead
46 }
47
48 private:
49 std::queue<T> q;
50 mutable std::mutex mutex;
51 };