1 #ifndef FTXUI_COMPONENT_RECEIVER_HPP_
2 #define FTXUI_COMPONENT_RECEIVER_HPP_
6 #include <condition_variable>
44 template<
class T>
using Sender = std::unique_ptr<SenderImpl<T>>;
45 template<
class T>
using Receiver = std::unique_ptr<ReceiverImpl<T>>;
54 void Send(T t) { receiver_->Receive(std::move(t)); }
69 std::unique_lock<std::mutex> lock(mutex_);
71 return std::unique_ptr<SenderImpl<T>>(
new SenderImpl<T>(
this));
76 while (senders_ || !queue_.empty()) {
77 std::unique_lock<std::mutex> lock(mutex_);
82 *t = std::move(queue_.front());
90 std::unique_lock<std::mutex> lock(mutex_);
91 return !queue_.empty();
99 std::unique_lock<std::mutex> lock(mutex_);
100 queue_.push(std::move(t));
102 notifier_.notify_one();
105 void ReleaseSender() {
107 notifier_.notify_one();
111 std::queue<T> queue_;
112 std::condition_variable notifier_;
113 std::atomic<int> senders_;
118 return std::make_unique<ReceiverImpl<T>>();
123 #endif // FTXUI_COMPONENT_RECEIVER_HPP_