1//
2// wait_for_all.cpp
3// ~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6//
7// Distributed under the Boost Software License, Version 1.0. (See accompanying
8// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9//
10
11#include <boost/asio.hpp>
12#include <boost/asio/experimental/deferred.hpp>
13#include <boost/asio/experimental/parallel_group.hpp>
14#include <iostream>
15
16#ifdef BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
17
18int main()
19{
20 boost::asio::io_context ctx;
21
22 boost::asio::posix::stream_descriptor in(ctx, ::dup(STDIN_FILENO));
23 boost::asio::steady_timer timer(ctx, std::chrono::seconds(5));
24
25 char data[1024];
26
27 boost::asio::experimental::make_parallel_group(
28 ops: in.async_read_some(
29 buffers: boost::asio::buffer(data),
30 token: boost::asio::deferred),
31 ops: timer.async_wait(
32 token: boost::asio::deferred)
33 ).async_wait(
34 cancellation_condition: boost::asio::experimental::wait_for_all(),
35 token: [](
36 std::array<std::size_t, 2> completion_order,
37 boost::system::error_code ec1, std::size_t n1,
38 boost::system::error_code ec2
39 )
40 {
41 switch (completion_order[0])
42 {
43 case 0:
44 {
45 std::cout << "descriptor finished: " << ec1 << ", " << n1 << "\n";
46 }
47 break;
48 case 1:
49 {
50 std::cout << "timer finished: " << ec2 << "\n";
51 }
52 break;
53 }
54 }
55 );
56
57 ctx.run();
58}
59
60#else // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
61int main() {}
62#endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
63

source code of boost/libs/asio/example/cpp11/parallel_group/wait_for_all.cpp