MCPcopy Create free account
hub / github.com/NVIDIA/DALI / ThreadSafeQueue

Class ThreadSafeQueue

dali/util/thread_safe_queue.h:27–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25
26template<typename T>
27class ThreadSafeQueue {
28 public:
29 void push(T item) {
30 {
31 std::lock_guard<std::mutex> lock(lock_);
32 queue_.push(std::move(item));
33 }
34 cond_.notify_one();
35 }
36
37 T pop() {
38 std::unique_lock<std::mutex> lock{lock_};
39 cond_.wait(lock, [&](){return !queue_.empty() || interrupt_;});
40 if (interrupt_) {
41 return {};
42 }
43 T item = std::move(queue_.front());
44 queue_.pop();
45 return item;
46 }
47
48 const T& peek() {
49 return front();
50 }
51
52 T& front() {
53 static auto int_return = T{};
54 std::unique_lock<std::mutex> lock{lock_};
55 cond_.wait(lock, [&](){return !queue_.empty() || interrupt_;});
56 if (interrupt_) {
57 return int_return;
58 }
59 return queue_.front();
60 }
61
62 bool empty() const {
63 return queue_.empty();
64 }
65
66 typename std::queue<T>::size_type size() const {
67 return queue_.size();
68 }
69
70 void shutdown() {
71 interrupt_ = true;
72 cond_.notify_all();
73 }
74
75 private:
76 std::queue<T> queue_;
77 std::mutex lock_;
78 std::condition_variable cond_;
79 bool interrupt_ = false;
80};
81
82} // namespace dali
83

Callers

nothing calls this directly

Calls 4

frontFunction · 0.85
waitMethod · 0.45
emptyMethod · 0.45
frontMethod · 0.45

Tested by

no test coverage detected