Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Channels

[Note] Note

This is an experimental feature.

The templates experimental::basic_channel and experimental::basic_concurrent_channel, with aliases experimental::channel and experimental::concurrent_channel, may be used to send messages between different parts of the same application. A message is defined as a collection of arguments to be passed to a completion handler, and the set of messages supported by a channel is specified by its template parameters. Messages may be sent and received using asynchronous or non-blocking synchronous operations.

For example:

// Create a channel with no buffer space.
channel<void(error_code, size_t)> ch(ctx);

// The call to try_send fails as there is no buffer
// space and no waiting receive operations.
bool ok = ch.try_send(boost::asio::error::eof, 123);
assert(!ok);

// The async_send operation is outstanding until
// a receive operation consumes the message.
ch.async_send(boost::asio::error::eof, 123,
    [](error_code ec)
    {
      // ...
    });

// The async_receive consumes the message. Both the
// async_send and async_receive operations complete
// immediately.
ch.async_receive(
    [](error_code ec, size_t n)
    {
      // ...
    });
See Also

experimental::basic_channel, experimental::basic_concurrent_channel, Channels examples (C++20).


PrevUpHomeNext