1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Futures</title> 5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../boost_asio.html" title="Boost.Asio"> 8<link rel="up" href="../cpp2011.html" title="C++ 2011 Support"> 9<link rel="prev" href="chrono.html" title="Chrono"> 10<link rel="next" href="../implementation.html" title="Platform-Specific Implementation Notes"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="chrono.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../cpp2011.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../implementation.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_asio.overview.cpp2011.futures"></a><a class="link" href="futures.html" title="Futures">Futures</a> 28</h4></div></div></div> 29<p> 30 The <code class="computeroutput">boost::asio::use_future</code> special value provides first-class 31 support for returning a C++11 <code class="computeroutput">std::future</code> from an asynchronous 32 operation's initiating function. 33 </p> 34<p> 35 To use <code class="computeroutput">boost::asio::use_future</code>, pass it to an asynchronous 36 operation instead of a normal completion handler. For example: 37 </p> 38<pre class="programlisting">std::future<std::size_t> length = 39 my_socket.async_read_some(my_buffer, boost::asio::use_future); 40</pre> 41<p> 42 Where a handler signature has the form: 43 </p> 44<pre class="programlisting">void handler(boost::system::error_code ec, result_type result); 45</pre> 46<p> 47 the initiating function returns a <code class="computeroutput">std::future</code> templated on 48 <code class="computeroutput">result_type</code>. In the above example, this is <code class="computeroutput">std::size_t</code>. 49 If the asynchronous operation fails, the <code class="computeroutput">error_code</code> is converted 50 into a <code class="computeroutput">system_error</code> exception and passed back to the caller 51 through the future. 52 </p> 53<p> 54 Where a handler signature has the form: 55 </p> 56<pre class="programlisting">void handler(boost::system::error_code ec); 57</pre> 58<p> 59 the initiating function returns <code class="computeroutput">std::future<void></code>. As 60 above, an error is passed back in the future as a <code class="computeroutput">system_error</code> 61 exception. 62 </p> 63<p> 64 <a class="link" href="../../reference/use_future.html" title="use_future">use_future</a>, <a class="link" href="../../reference/use_future_t.html" title="use_future_t">use_future_t</a>, <a class="link" href="../../examples/cpp11_examples.html#boost_asio.examples.cpp11_examples.futures">Futures example (C++11)</a>. 65 </p> 66</div> 67<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 68<td align="left"></td> 69<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 70 Kohlhoff<p> 71 Distributed under the Boost Software License, Version 1.0. (See accompanying 72 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>) 73 </p> 74</div></td> 75</tr></table> 76<hr> 77<div class="spirit-nav"> 78<a accesskey="p" href="chrono.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../cpp2011.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../implementation.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 79</div> 80</body> 81</html> 82