]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/libs/asio/example/cpp14/executors/async_1.cpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / libs / asio / example / cpp14 / executors / async_1.cpp
diff --git a/ceph/src/boost/libs/asio/example/cpp14/executors/async_1.cpp b/ceph/src/boost/libs/asio/example/cpp14/executors/async_1.cpp
new file mode 100644 (file)
index 0000000..db72563
--- /dev/null
@@ -0,0 +1,47 @@
+#include <boost/asio/ts/executor.hpp>
+#include <boost/asio/thread_pool.hpp>
+#include <iostream>
+#include <string>
+
+using boost::asio::bind_executor;
+using boost::asio::dispatch;
+using boost::asio::make_work_guard;
+using boost::asio::post;
+using boost::asio::thread_pool;
+
+// A function to asynchronously read a single line from an input stream.
+template <class Handler>
+void async_getline(std::istream& is, Handler handler)
+{
+  // Create executor_work for the handler's associated executor.
+  auto work = make_work_guard(handler);
+
+  // Post a function object to do the work asynchronously.
+  post([&is, work, 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(),
+            [line=std::move(line), handler=std::move(handler)]() mutable
+            {
+              handler(std::move(line));
+            });
+      });
+}
+
+int main()
+{
+  thread_pool pool;
+
+  std::cout << "Enter a line: ";
+
+  async_getline(std::cin,
+      bind_executor(pool, [](std::string line)
+        {
+          std::cout << "Line: " << line << "\n";
+        }));
+
+  pool.join();
+}