A channel for messages.
template< typename Executor, typename Traits, typename... Signatures> class basic_channel
Name |
Description |
---|---|
Rebinds the channel type to another executor. |
|
The type of the executor associated with the channel. |
|
The traits type associated with the channel. |
Name |
Description |
---|---|
Asynchronously receive a message. |
|
Asynchronously send a message. |
|
basic_channel [constructor] |
Construct a basic_channel. |
Cancel all asynchronous operations waiting on the channel. |
|
Get the capacity of the channel's buffer. |
|
Close the channel. |
|
Get the executor associated with the object. |
|
Determine whether the channel is open. |
|
Move-assign a basic_channel from another. |
|
Determine whether a message can be received without blocking. |
|
Reset the channel to its initial state. |
|
Try to receive a message without blocking. |
|
Try to send a message without blocking. |
|
Try to send a number of messages without blocking. |
|
~basic_channel [destructor] |
Destructor. |
The experimental::basic_channel
class template is
used for sending 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 Traits
and Signatures...
template parameters. Messages may be sent and received using asynchronous
or non-blocking synchronous operations.
Unless customising the traits, applications will typically use the experimental::channel
alias template. For example:
void send_loop(int i, steady_timer& timer, channel<void(error_code, int)>& ch) { if (i < 10) { timer.expires_after(chrono::seconds(1)); timer.async_wait( [i, &timer, &ch](error_code error) { if (!error) { ch.async_send(error_code(), i, [i, &timer, &ch](error_code error) { if (!error) { send_loop(i + 1, timer, ch); } }); } }); } else { ch.close(); } } void receive_loop(channel<void(error_code, int)>& ch) { ch.async_receive( [&ch](error_code error, int i) { if (!error) { std::cout << "Received " << i << "\n"; receive_loop(ch); } }); }
Distinct objects: Safe.
Shared objects: Unsafe.
The experimental::basic_channel
class template is
not thread-safe, and would typically be used for passing messages between
application code that runs on the same thread or in the same strand. Consider
using experimental::basic_concurrent_channel
, and its
alias template experimental::concurrent_channel
,
to pass messages between code running in different threads.
Header: asio/experimental/basic_channel.hpp
Convenience header: None