4 #ifndef FTXUI_COMPONENT_RECEIVER_HPP_
5 #define FTXUI_COMPONENT_RECEIVER_HPP_
9 #include <condition_variable>
42 template<
class T>
class SenderImpl;
43 template<
class T>
class ReceiverImpl;
45 template<
class T>
using Sender = std::unique_ptr<SenderImpl<T>>;
46 template<
class T>
using Receiver = std::unique_ptr<ReceiverImpl<T>>;
59 void Send(T t) { receiver_->Receive(std::move(t)); }
74 std::unique_lock<std::mutex> lock(mutex_);
76 return std::unique_ptr<SenderImpl<T>>(
new SenderImpl<T>(
this));
81 while (senders_ || !queue_.empty()) {
82 std::unique_lock<std::mutex> lock(mutex_);
89 *t = std::move(queue_.front());
97 std::unique_lock<std::mutex> lock(mutex_);
107 std::unique_lock<std::mutex> lock(mutex_);
108 return !queue_.empty();
112 std::unique_lock<std::mutex> lock(mutex_);
113 return queue_.empty() && !senders_;
121 std::unique_lock<std::mutex> lock(mutex_);
122 queue_.push(std::move(t));
124 notifier_.notify_one();
127 void ReleaseSender() {
129 notifier_.notify_one();
133 std::queue<T> queue_;
134 std::condition_variable notifier_;
135 std::atomic<int> senders_{0};
140 return std::make_unique<ReceiverImpl<T>>();
bool ReceiveNonBlocking(T *t)
friend class SenderImpl< T >
friend class ReceiverImpl< T >
SenderImpl(SenderImpl &&)=delete
SenderImpl(const SenderImpl &)=delete
SenderImpl & operator=(const SenderImpl &)=delete
SenderImpl & operator=(SenderImpl &&)=delete
Receiver< T > MakeReceiver()
std::unique_ptr< ReceiverImpl< T > > Receiver
std::unique_ptr< SenderImpl< T > > Sender