| 1074 | cv.notify_all(); |
| 1075 | } |
| 1076 | bool read(T & output, const std::function<bool()> & should_stop) { |
| 1077 | std::unique_lock<std::mutex> lk(mutex); |
| 1078 | constexpr auto poll_interval = std::chrono::milliseconds(500); |
| 1079 | while (true) { |
| 1080 | if (!queue.empty()) { |
| 1081 | output = std::move(queue.front()); |
| 1082 | queue.pop(); |
| 1083 | return true; |
| 1084 | } |
| 1085 | if (writer_closed.load()) { |
| 1086 | return false; // clean EOF |
| 1087 | } |
| 1088 | if (should_stop()) { |
| 1089 | close_read(); // signal broken pipe to writer |
| 1090 | return false; // cancelled / reader no longer alive |
| 1091 | } |
| 1092 | cv.wait_for(lk, poll_interval); |
| 1093 | } |
| 1094 | } |
| 1095 | bool write(T && data) { |
| 1096 | std::lock_guard<std::mutex> lk(mutex); |
| 1097 | if (reader_closed.load()) { |