87 lines
7.3 KiB
HTML
87 lines
7.3 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
<title>Deferred Operations</title>
|
|
<link rel="stylesheet" href="../../../boostbook.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
|
|
<link rel="home" href="../../../index.html" title="Asio">
|
|
<link rel="up" href="../composition.html" title="Composition and Completion Tokens">
|
|
<link rel="prev" href="coro.html" title="Resumable C++ 20 Coroutines">
|
|
<link rel="next" href="promises.html" title="Promises">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="asio C++ library" width="250" height="60" src="../../../asio.png"></td></tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="coro.html"><img src="../../../prev.png" alt="Prev"></a><a accesskey="u" href="../composition.html"><img src="../../../up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../home.png" alt="Home"></a><a accesskey="n" href="promises.html"><img src="../../../next.png" alt="Next"></a>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="asio.overview.composition.deferred"></a><a class="link" href="deferred.html" title="Deferred Operations">Deferred Operations</a>
|
|
</h4></div></div></div>
|
|
<p>
|
|
The <a class="link" href="../../reference/deferred.html" title="deferred"><code class="computeroutput"><span class="identifier">deferred</span></code></a>,
|
|
completion token takes a call to an asynchronous operation's initiating
|
|
function and turns it into a function object that accepts a completion
|
|
token. For example:
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">auto</span> <span class="identifier">deferred_op</span> <span class="special">=</span>
|
|
<span class="identifier">timer</span><span class="special">.</span><span class="identifier">async_wait</span><span class="special">(</span>
|
|
<span class="identifier">asio</span><span class="special">::</span><span class="identifier">deferred</span><span class="special">);</span>
|
|
<span class="special">...</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">deferred_op</span><span class="special">)(</span>
|
|
<span class="special">[](</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">){</span> <span class="special">...</span> <span class="special">});</span>
|
|
</pre>
|
|
<p>
|
|
or:
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">auto</span> <span class="identifier">deferred_op</span> <span class="special">=</span>
|
|
<span class="identifier">timer</span><span class="special">.</span><span class="identifier">async_wait</span><span class="special">(</span>
|
|
<span class="identifier">asio</span><span class="special">::</span><span class="identifier">deferred</span><span class="special">);</span>
|
|
<span class="special">...</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">future</span><span class="special"><</span><span class="keyword">void</span><span class="special">></span> <span class="special">=</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">deferred_op</span><span class="special">)(</span>
|
|
<span class="identifier">asio</span><span class="special">::</span><span class="identifier">use_future</span><span class="special">);</span>
|
|
</pre>
|
|
<p>
|
|
The deferred token also supports chaining, to create simple compositions:
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">auto</span> <span class="identifier">deferred_op</span> <span class="special">=</span>
|
|
<span class="identifier">timer</span><span class="special">.</span><span class="identifier">async_wait</span><span class="special">(</span>
|
|
<span class="identifier">asio</span><span class="special">::</span><span class="identifier">deferred</span><span class="special">(</span>
|
|
<span class="special">[&](</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">)</span>
|
|
<span class="special">{</span>
|
|
<span class="identifier">timer</span><span class="special">.</span><span class="identifier">expires_after</span><span class="special">(</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">seconds</span><span class="special">(</span><span class="number">1</span><span class="special">));</span>
|
|
|
|
<span class="keyword">return</span> <span class="identifier">timer</span><span class="special">.</span><span class="identifier">async_wait</span><span class="special">(</span>
|
|
<span class="identifier">asio</span><span class="special">::</span><span class="identifier">deferred</span><span class="special">);</span>
|
|
<span class="special">});</span>
|
|
<span class="special">...</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">future</span><span class="special"><</span><span class="keyword">void</span><span class="special">></span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">deferred_op</span><span class="special">)(</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">use_future</span><span class="special">);</span>
|
|
</pre>
|
|
<h6>
|
|
<a name="asio.overview.composition.deferred.h0"></a>
|
|
<span><a name="asio.overview.composition.deferred.see_also"></a></span><a class="link" href="deferred.html#asio.overview.composition.deferred.see_also">See
|
|
Also</a>
|
|
</h6>
|
|
<p>
|
|
<a class="link" href="../../reference/deferred.html" title="deferred">deferred</a>, <a class="link" href="../../reference/deferred_t.html" title="deferred_t">deferred_t</a>,
|
|
<a class="link" href="../../examples/cpp14_examples.html#asio.examples.cpp14_examples.deferred">Deferred examples
|
|
(C++11)</a>, <a class="link" href="../../examples/cpp14_examples.html#asio.examples.cpp14_examples.deferred">Deferred
|
|
examples (C++14)</a>.
|
|
</p>
|
|
</div>
|
|
<div class="copyright-footer">Copyright © 2003-2023 Christopher M. Kohlhoff<p>
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
|
|
</p>
|
|
</div>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="coro.html"><img src="../../../prev.png" alt="Prev"></a><a accesskey="u" href="../composition.html"><img src="../../../up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../home.png" alt="Home"></a><a accesskey="n" href="promises.html"><img src="../../../next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|