]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp14/executors/async_2.cpp
6f40a902c8ada28e8e4aaca84bd22d7fcd32cc9d
1 #include <boost/asio/ts/executor.hpp>
2 #include <boost/asio/thread_pool.hpp>
6 using boost::asio::bind_executor
;
7 using boost::asio::dispatch
;
8 using boost::asio::get_associated_executor
;
9 using boost::asio::make_work_guard
;
10 using boost::asio::post
;
11 using boost::asio::thread_pool
;
13 // A function to asynchronously read a single line from an input stream.
14 template <class Handler
>
15 void async_getline(std::istream
& is
, Handler handler
)
17 // Create executor_work for the handler's associated executor.
18 auto work
= make_work_guard(handler
);
20 // Post a function object to do the work asynchronously.
21 post([&is
, work
, handler
=std::move(handler
)]() mutable
24 std::getline(is
, line
);
26 // Pass the result to the handler, via the associated executor.
27 dispatch(work
.get_executor(),
28 [line
=std::move(line
), handler
=std::move(handler
)]() mutable
30 handler(std::move(line
));
35 // A function to asynchronously read multiple lines from an input stream.
36 template <class Handler
>
37 void async_getlines(std::istream
& is
, std::string init
, Handler handler
)
39 // Get the final handler's associated executor.
40 auto ex
= get_associated_executor(handler
);
42 // Use the associated executor for each operation in the composition.
45 [&is
, lines
=std::move(init
), handler
=std::move(handler
)]
46 (std::string line
) mutable
51 async_getlines(is
, lines
+ line
+ "\n", std::move(handler
));
59 std::cout
<< "Enter text, terminating with a blank line:\n";
61 async_getlines(std::cin
, "",
62 bind_executor(pool
, [](std::string lines
)
64 std::cout
<< "Lines:\n" << lines
<< "\n";