4 #include <condition_variable>
54 std::optional<value_type>
try_receive(
int timeout_millis = 0) {
55 std::unique_lock<std::mutex> lock{mtx_};
57 if (timeout_millis <= 0) {
58 cond_var_.wait(lock, [
this] {
return buffer_.size() > 0 ||
closed(); });
60 cond_var_.wait_for(lock, std::chrono::milliseconds(timeout_millis),
61 [
this] {
return buffer_.size() > 0 ||
closed(); });
64 if (buffer_.empty()) {
68 auto entry = std::move(buffer_.front());
71 cond_var_.notify_one();
77 [[nodiscard]]
size_type constexpr
size() const noexcept {
return buffer_.size(); }
81 [[nodiscard]]
bool constexpr
empty() const noexcept {
return buffer_.empty(); }
85 cond_var_.notify_one();
86 is_closed_.store(
true);
91 [[nodiscard]]
bool closed() const noexcept {
return is_closed_.load(); }
95 std::queue<value_type> buffer_;
96 std::atomic<bool> is_closed_;
97 std::condition_variable cond_var_;
100 bool do_send(
auto &&payload) {
101 if (is_closed_.load()) {
105 std::unique_lock<std::mutex> lock(mtx_);
106 if (capacity_ > 0 && buffer_.size() >= capacity_) {
107 cond_var_.wait(lock, [
this]() {
return buffer_.size() < capacity_; });
110 buffer_.push(std::forward<decltype(payload)>(payload));
111 cond_var_.notify_one();
Thread-safe communication channel data type.
Definition: channel.h:24
Channel(Channel &&)=delete
Channel & operator=(Channel &&)=delete
void close() noexcept
Close the channel, no new messages are accepted.
Definition: channel.h:84
virtual ~Channel()=default
Destroys a Channel instance.
Channel(const Channel &)=delete
bool send(value_type &&message)
Sends a new message through the channel.
Definition: channel.h:49
constexpr size_type size() const noexcept
Gets the current channel size, number of messages.
Definition: channel.h:77
Channel(size_type capacity=0)
Initialises a new instance of the Channel class.
Definition: channel.h:31
bool send(const value_type &message)
Sends a new message through the channel by reference.
Definition: channel.h:44
std::optional< value_type > try_receive(int timeout_millis=0)
Try to receive a message from the channel.
Definition: channel.h:54
Channel & operator=(const Channel &)=delete
std::size_t size_type
Definition: channel.h:27
T value_type
Definition: channel.h:26
constexpr bool empty() const noexcept
Determine whether the channel is empty.
Definition: channel.h:81
bool closed() const noexcept
Determine whether the channel is closed.
Definition: channel.h:91
Top-level namespace for Health-GPS C++ API.
Definition: analysis_definition.h:8