1 | // |
2 | // ranged_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/parallel_group.hpp> |
13 | #include <iostream> |
14 | #include <vector> |
15 | |
16 | #ifdef BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR |
17 | |
18 | int main() |
19 | { |
20 | boost::asio::io_context ctx; |
21 | |
22 | boost::asio::posix::stream_descriptor out(ctx, ::dup(STDOUT_FILENO)); |
23 | boost::asio::posix::stream_descriptor err(ctx, ::dup(STDERR_FILENO)); |
24 | |
25 | using op_type = decltype( |
26 | out.async_write_some( |
27 | buffers: boost::asio::buffer(data: "" , max_size_in_bytes: 0), |
28 | token: boost::asio::deferred |
29 | ) |
30 | ); |
31 | |
32 | std::vector<op_type> ops; |
33 | |
34 | ops.push_back( |
35 | x: out.async_write_some( |
36 | buffers: boost::asio::buffer(data: "first\r\n" , max_size_in_bytes: 7), |
37 | token: boost::asio::deferred |
38 | ) |
39 | ); |
40 | |
41 | ops.push_back( |
42 | x: err.async_write_some( |
43 | buffers: boost::asio::buffer(data: "second\r\n" , max_size_in_bytes: 8), |
44 | token: boost::asio::deferred |
45 | ) |
46 | ); |
47 | |
48 | boost::asio::experimental::make_parallel_group(range&: ops).async_wait( |
49 | cancellation_condition: boost::asio::experimental::wait_for_all(), |
50 | token: []( |
51 | std::vector<std::size_t> completion_order, |
52 | std::vector<boost::system::error_code> ec, |
53 | std::vector<std::size_t> n |
54 | ) |
55 | { |
56 | for (std::size_t i = 0; i < completion_order.size(); ++i) |
57 | { |
58 | std::size_t idx = completion_order[i]; |
59 | std::cout << "operation " << idx << " finished: " ; |
60 | std::cout << ec[idx] << ", " << n[idx] << "\n" ; |
61 | } |
62 | } |
63 | ); |
64 | |
65 | ctx.run(); |
66 | } |
67 | |
68 | #else // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) |
69 | int main() {} |
70 | #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) |
71 | |