MCPcopy Create free account
hub / github.com/decoder-it/ADCSCoercePotato / BlockingQueue

Class BlockingQueue

BlockingQueue.h:7–40  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5
6typedef std::mutex Mutex;
7template<typename ITEM> class BlockingQueue{
8public:
9 void push(const ITEM& value) { // push
10 std::lock_guard<Mutex> lock(mutex);
11 queue.push(std::move(value));
12 condition.notify_one();
13 }
14 bool try_pop(ITEM& value) { // non-blocking pop
15 std::lock_guard<Mutex> lock(mutex);
16 if (queue.empty()) return false;
17 value = std::move(queue.front());
18 queue.pop();
19 return true;
20 }
21 ITEM wait_pop() { // blocking pop
22 std::unique_lock<Mutex> lock(mutex);
23 condition.wait(lock, [this] {return !queue.empty(); });
24 ITEM const value = std::move(queue.front());
25 queue.pop();
26 return value;
27 }
28 bool empty() const { // queue is empty?
29 std::lock_guard<Mutex> lock(mutex);
30 return queue.empty();
31 }
32 void clear() { // remove all items
33 ITEM item;
34 while (try_pop(item));
35 }
36private:
37 Mutex mutex;
38 std::queue<ITEM> queue;
39 std::condition_variable condition;
40};
41

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected