summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/examples/cpp14/deferred/deferred_5.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/asio/src/examples/cpp14/deferred/deferred_5.cpp')
-rw-r--r--3rdparty/asio/src/examples/cpp14/deferred/deferred_5.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/3rdparty/asio/src/examples/cpp14/deferred/deferred_5.cpp b/3rdparty/asio/src/examples/cpp14/deferred/deferred_5.cpp
new file mode 100644
index 00000000000..b48056bd66d
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp14/deferred/deferred_5.cpp
@@ -0,0 +1,67 @@
+//
+// deferred_5.cpp
+// ~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <asio.hpp>
+#include <asio/experimental/deferred.hpp>
+#include <iostream>
+
+using asio::experimental::deferred;
+
+template <typename CompletionToken>
+auto async_wait_twice(asio::steady_timer& timer, CompletionToken&& token)
+{
+ return timer.async_wait(
+ deferred(
+ [&](std::error_code ec)
+ {
+ std::cout << "first timer wait finished: " << ec.message() << "\n";
+ timer.expires_after(std::chrono::seconds(1));
+ return deferred.when(!ec)
+ .then(timer.async_wait(deferred))
+ .otherwise(deferred.values(ec));
+ }
+ )
+ )(
+ deferred(
+ [&](std::error_code ec)
+ {
+ std::cout << "second timer wait finished: " << ec.message() << "\n";
+ return deferred.when(!ec)
+ .then(deferred.values(42))
+ .otherwise(deferred.values(0));
+ }
+ )
+ )(
+ std::forward<CompletionToken>(token)
+ );
+}
+
+int main()
+{
+ asio::io_context ctx;
+
+ asio::steady_timer timer(ctx);
+ timer.expires_after(std::chrono::seconds(1));
+
+ async_wait_twice(
+ timer,
+ [](int result)
+ {
+ std::cout << "result is " << result << "\n";
+ }
+ );
+
+ // Uncomment the following line to trigger an error in async_wait_twice.
+ //timer.cancel();
+
+ ctx.run();
+
+ return 0;
+}