]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/libs/asio/example/cpp14/executors/async_2.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / asio / example / cpp14 / executors / async_2.cpp
index 6f40a902c8ada28e8e4aaca84bd22d7fcd32cc9d..1987c3b51499f4ea0c3b54782956a71f5c9ec3fc 100644 (file)
@@ -1,30 +1,35 @@
-#include <boost/asio/ts/executor.hpp>
-#include <boost/asio/thread_pool.hpp>
+#include <boost/asio/associated_executor.hpp>
+#include <boost/asio/bind_executor.hpp>
+#include <boost/asio/execution.hpp>
+#include <boost/asio/static_thread_pool.hpp>
 #include <iostream>
 #include <string>
 
 using boost::asio::bind_executor;
-using boost::asio::dispatch;
 using boost::asio::get_associated_executor;
-using boost::asio::make_work_guard;
-using boost::asio::post;
-using boost::asio::thread_pool;
+using boost::asio::static_thread_pool;
+namespace execution = boost::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 = boost::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
+  execution::execute(
+      boost::asio::require(io_ex, execution::blocking.never),
+      [&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(),
+        execution::execute(
+            boost::asio::prefer(work_ex, execution::blocking.possibly),
             [line=std::move(line), handler=std::move(handler)]() mutable
             {
               handler(std::move(line));
@@ -33,36 +38,44 @@ void async_getline(std::istream& is, Handler handler)
 }
 
 // A function to asynchronously read multiple lines from an input stream.
-template <class Handler>
-void async_getlines(std::istream& is, std::string init, Handler handler)
+template <class IoExecutor, class Handler>
+void async_getlines(IoExecutor io_ex, std::istream& is, std::string init, Handler handler)
 {
-  // Get the final handler's associated executor.
-  auto ex = get_associated_executor(handler);
+  // Track work for the I/O executor.
+  auto io_work_ex = boost::asio::prefer(io_ex,
+      execution::outstanding_work.tracked);
+
+  // Track work for the handler's associated executor.
+  auto handler_work_ex = boost::asio::prefer(
+      get_associated_executor(handler, io_ex),
+      execution::outstanding_work.tracked);
 
   // Use the associated executor for each operation in the composition.
-  async_getline(is,
-      bind_executor(ex,
-        [&is, lines=std::move(init), handler=std::move(handler)]
+  async_getline(io_work_ex, is,
+      bind_executor(handler_work_ex,
+        [io_work_ex, &is, lines=std::move(init), handler=std::move(handler)]
         (std::string line) mutable
         {
           if (line.empty())
             handler(lines);
           else
-            async_getlines(is, lines + line + "\n", std::move(handler));
+            async_getlines(io_work_ex, is, lines + line + "\n", std::move(handler));
         }));
 }
 
 int main()
 {
-  thread_pool pool;
+  static_thread_pool io_pool(1);
+  static_thread_pool completion_pool(1);
 
   std::cout << "Enter text, terminating with a blank line:\n";
 
-  async_getlines(std::cin, "",
-      bind_executor(pool, [](std::string lines)
+  async_getlines(io_pool.executor(), std::cin, "",
+      bind_executor(completion_pool.executor(), [](std::string lines)
         {
           std::cout << "Lines:\n" << lines << "\n";
         }));
 
-  pool.join();
+  io_pool.wait();
+  completion_pool.wait();
 }