MCPcopy Create free account
hub / github.com/OpenMined/TenSEAL / blocking_queue

Class blocking_queue

tenseal/cpp/utils/queue.h:19–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17 **/
18template <class T>
19class blocking_queue {
20 public:
21 /**
22 * push() appends a new item to the queue and notifies the "pop" listener
23 *about the event.
24 **/
25 template <class... Args>
26 void push(Args&&... args) {
27 {
28 std::scoped_lock lock{mutex_};
29 queue_.emplace(std::forward<Args>(args)...);
30 }
31 ready_.notify_one();
32 }
33 /**
34 * pop() waits until an item is available in the queue, pops it out and
35 *assigns it to the "out" parameter.
36 **/
37 [[nodiscard]] bool pop(T& out) {
38 std::unique_lock lock{mutex_};
39 ready_.wait(lock, [this] { return !queue_.empty() || done_; });
40 if (queue_.empty()) return false;
41
42 out = std::move(queue_.front());
43 queue_.pop();
44
45 return true;
46 }
47 /**
48 * done() notifies all listeners to shutdown.
49 **/
50 void done() noexcept {
51 {
52 std::scoped_lock lock{mutex_};
53 done_ = true;
54 }
55 ready_.notify_all();
56 }
57 /**
58 * empty() returns if the queue is empty or not.
59 **/
60 [[nodiscard]] bool empty() const noexcept {
61 std::scoped_lock lock{mutex_};
62 return queue_.empty();
63 }
64 /**
65 * size() returns the size of the queue.
66 **/
67 [[nodiscard]] unsigned int size() const noexcept {
68 std::scoped_lock lock{mutex_};
69 return queue_.size();
70 }
71
72 private:
73 std::queue<T> queue_;
74 std::condition_variable ready_;
75 std::mutex mutex_;
76 bool done_{false};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected