Channel for communication between coroutines.
More...
#include <asyncpp/channel.h>
|
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.
|
|
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
-
◆ close()
Close the channel.
This resumes all currently blocked readers and writers and marks the channel as closed.
◆ is_closed()
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()
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()
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: