summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/examples/cpp14/executors/async_1.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/asio/src/examples/cpp14/executors/async_1.cpp')
-rw-r--r--3rdparty/asio/src/examples/cpp14/executors/async_1.cpp39
1 files changed, 23 insertions, 16 deletions
diff --git a/3rdparty/asio/src/examples/cpp14/executors/async_1.cpp b/3rdparty/asio/src/examples/cpp14/executors/async_1.cpp
index d282b74f206..0d1bdfcb8bd 100644
--- a/3rdparty/asio/src/examples/cpp14/executors/async_1.cpp
+++ b/3rdparty/asio/src/examples/cpp14/executors/async_1.cpp
@@ -1,29 +1,33 @@
-#include <asio/ts/executor.hpp>
-#include <asio/thread_pool.hpp>
+#include <asio/associated_executor.hpp>
+#include <asio/bind_executor.hpp>
+#include <asio/execution.hpp>
+#include <asio/static_thread_pool.hpp>
#include <iostream>
#include <string>
using asio::bind_executor;
-using asio::dispatch;
-using asio::make_work_guard;
-using asio::post;
-using asio::thread_pool;
+using asio::get_associated_executor;
+using asio::static_thread_pool;
+namespace execution = asio::execution;
// A function to asynchronously read a single line from an input stream.
-template <class Handler>
-void async_getline(std::istream& is, Handler handler)
+template <class IoExecutor, class Handler>
+void async_getline(IoExecutor io_ex, std::istream& is, Handler handler)
{
- // Create executor_work for the handler's associated executor.
- auto work = make_work_guard(handler);
+ // Track work for the handler's associated executor.
+ auto work_ex = asio::prefer(
+ get_associated_executor(handler, io_ex),
+ execution::outstanding_work.tracked);
// Post a function object to do the work asynchronously.
- post([&is, work, handler=std::move(handler)]() mutable
+ asio::require(io_ex, execution::blocking.never).execute(
+ [&is, work_ex, handler=std::move(handler)]() mutable
{
std::string line;
std::getline(is, line);
// Pass the result to the handler, via the associated executor.
- dispatch(work.get_executor(),
+ asio::prefer(work_ex, execution::blocking.possibly).execute(
[line=std::move(line), handler=std::move(handler)]() mutable
{
handler(std::move(line));
@@ -33,15 +37,18 @@ void async_getline(std::istream& is, Handler handler)
int main()
{
- thread_pool pool;
+ static_thread_pool io_pool(1);
+ static_thread_pool completion_pool(1);
std::cout << "Enter a line: ";
- async_getline(std::cin,
- bind_executor(pool, [](std::string line)
+ async_getline(io_pool.executor(), std::cin,
+ bind_executor(completion_pool.executor(),
+ [](std::string line)
{
std::cout << "Line: " << line << "\n";
}));
- pool.join();
+ io_pool.wait();
+ completion_pool.wait();
}