| 936 | cv.notify_all(); |
| 937 | } |
| 938 | bool read(T & output, const std::function<bool()> & should_stop) { |
| 939 | std::unique_lock<std::mutex> lk(mutex); |
| 940 | constexpr auto poll_interval = std::chrono::milliseconds(500); |
| 941 | while (true) { |
| 942 | if (!queue.empty()) { |
| 943 | output = std::move(queue.front()); |
| 944 | queue.pop(); |
| 945 | return true; |
| 946 | } |
| 947 | if (writer_closed.load()) { |
| 948 | return false; // clean EOF |
| 949 | } |
| 950 | if (should_stop()) { |
| 951 | close_read(); // signal broken pipe to writer |
| 952 | return false; // cancelled / reader no longer alive |
| 953 | } |
| 954 | cv.wait_for(lk, poll_interval); |
| 955 | } |
| 956 | } |
| 957 | bool write(T && data) { |
| 958 | std::lock_guard<std::mutex> lk(mutex); |
| 959 | if (reader_closed.load()) { |