Consumer: pop with blocking wait (Module 08: std::optional)
| 49 | |
| 50 | // Consumer: pop with blocking wait (Module 08: std::optional) |
| 51 | [[nodiscard]] std::optional<T> pop() { |
| 52 | std::unique_lock<std::mutex> lock(mux_); |
| 53 | cv_data_.wait(lock, [this] { return count_ > 0 || !active_; }); |
| 54 | if (count_ == 0) return std::nullopt; // Shutdown signal |
| 55 | |
| 56 | T item = buffer_[head_]; |
| 57 | head_ = (head_ + 1) % N; |
| 58 | --count_; |
| 59 | lock.unlock(); |
| 60 | cv_space_.notify_one(); |
| 61 | return item; |
| 62 | } |
| 63 | |
| 64 | void shutdown() { |
| 65 | { |
no outgoing calls