Async++ unknown
Async (co_await/co_return) code for C++
Loading...
Searching...
No Matches
asyncpp::channel< T > Class Template Reference

Channel for communication between coroutines. More...

#include <asyncpp/channel.h>

Classes

struct  read_awaiter
 
struct  write_awaiter
 

Public Member Functions

read_awaiter read ()
 Read from the channel.
 
std::optional< T > try_read ()
 Attempt to read a value without suspending. If the channel is closed or no writer is suspended this returns std::nullopt.
 
write_awaiter write (T value)
 Write to the channel.
 
bool try_write (T value)
 Attempt to write a value without suspending. If the channel is closed or no reader is suspended this returns false.
 
void close ()
 Close the channel.
 
bool is_closed () const noexcept
 Check if the channel is closed.
 

Detailed Description

template<typename T>
class asyncpp::channel< T >

Channel for communication between coroutines.

Note
Channels are not a queue. Writing to a channel that has no active reader will suspend until one calls read (or the channel is closed). This class allows passing values between multiple coroutines in a thread safe and convenient way. They work similar to a pipe/loopback socket.
Template Parameters
TType of the payload

Member Function Documentation

◆ close()

template<typename T >
void asyncpp::channel< T >::close ( )
inline

Close the channel.

This resumes all currently blocked readers and writers and marks the channel as closed.

◆ is_closed()

template<typename T >
bool asyncpp::channel< T >::is_closed ( ) const
inlinenoexcept

Check if the channel is closed.

Note
You can not assume any operation to succeed if this returns false, because the channel might get closed between this and the next call. However since a closed channel can never transition back to open, a returned true guarantees that any further calls with fail.
Returns
true if the channel is closed, false if not

◆ read()

template<typename T >
channel< T >::read_awaiter asyncpp::channel< T >::read ( )
inline

Read from the channel.

Suspends until write/try_write is called on a different coroutine or the channel is closed.

Returns
Awaiter for reading (resumes with std::optional<T>).

◆ write()

template<typename T >
channel< T >::write_awaiter asyncpp::channel< T >::write ( T value)
inline

Write to the channel.

Note
This will suspend until a reader is available to receive the value or the channel is closed.
Returns
Awaiter for reading (resumes with true if the value was received and false if the channel was closed).

The documentation for this class was generated from the following file: