4#ifndef FTXUI_COMPONENT_RECEIVER_HPP_
5#define FTXUI_COMPONENT_RECEIVER_HPP_
9#include <condition_variable>
44template<
class T>
class SenderImpl;
45template<
class T>
class ReceiverImpl;
47template<
class T>
using Sender = std::unique_ptr<SenderImpl<T>>;
48template<
class T>
using Receiver = std::unique_ptr<ReceiverImpl<T>>;
57 void Send(T t) { receiver_->Receive(std::move(t)); }
72 std::unique_lock<std::mutex> lock(mutex_);
74 return std::unique_ptr<SenderImpl<T>>(
new SenderImpl<T>(
this));
79 while (senders_ || !queue_.empty()) {
80 std::unique_lock<std::mutex> lock(mutex_);
85 *t = std::move(queue_.front());
93 std::unique_lock<std::mutex> lock(mutex_);
102 std::unique_lock<std::mutex> lock(mutex_);
103 return !queue_.empty();
107 std::unique_lock<std::mutex> lock(mutex_);
108 return queue_.empty() && !senders_;
116 std::unique_lock<std::mutex> lock(mutex_);
117 queue_.push(std::move(t));
119 notifier_.notify_one();
122 void ReleaseSender() {
124 notifier_.notify_one();
128 std::queue<T> queue_;
129 std::condition_variable notifier_;
130 std::atomic<int> senders_;
135 return std::make_unique<ReceiverImpl<T>>();
bool ReceiveNonBlocking(T *t)
friend class SenderImpl< T >
friend class ReceiverImpl< T >
Receiver< T > MakeReceiver()
std::unique_ptr< ReceiverImpl< T > > Receiver
std::unique_ptr< SenderImpl< T > > Sender