3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Return Errorcode
</title>
5 <link rel=
"stylesheet" href=
"../../../../../../doc/src/boostbook.css" type=
"text/css">
6 <meta name=
"generator" content=
"DocBook XSL Stylesheets V1.75.2">
7 <link rel=
"home" href=
"../../index.html" title=
"Chapter 1. Fiber">
8 <link rel=
"up" href=
"../callbacks.html" title=
"Integrating Fibers with Asynchronous Callbacks">
9 <link rel=
"prev" href=
"overview.html" title=
"Overview">
10 <link rel=
"next" href=
"success_or_exception.html" title=
"Success or Exception">
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>
22 <div class=
"spirit-nav">
23 <a accesskey=
"p" href=
"overview.html"><img src=
"../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../callbacks.html"><img src=
"../../../../../../doc/src/images/up.png" alt=
"Up"></a><a accesskey=
"h" href=
"../../index.html"><img src=
"../../../../../../doc/src/images/home.png" alt=
"Home"></a><a accesskey=
"n" href=
"success_or_exception.html"><img src=
"../../../../../../doc/src/images/next.png" alt=
"Next"></a>
26 <div class=
"titlepage"><div><div><h3 class=
"title">
27 <a name=
"fiber.callbacks.return_errorcode"></a><a class=
"link" href=
"return_errorcode.html" title=
"Return Errorcode">Return Errorcode
</a>
28 </h3></div></div></div>
30 The
<code class=
"computeroutput"><span class=
"identifier">AsyncAPI
</span><span class=
"special">::
</span><span class=
"identifier">init_write
</span><span class=
"special">()
</span></code>
31 callback passes only an
<code class=
"computeroutput"><span class=
"identifier">errorcode
</span></code>.
32 If we simply want the blocking wrapper to return that
<code class=
"computeroutput"><span class=
"identifier">errorcode
</span></code>,
33 this is an extremely straightforward use of
<a class=
"link" href=
"../synchronization/futures/promise.html#class_promise"><code class=
"computeroutput">promise
<></code></a> and
34 <a class=
"link" href=
"../synchronization/futures/future.html#class_future"><code class=
"computeroutput">future
<></code></a>:
38 <pre class=
"programlisting"><span class=
"identifier">AsyncAPI
</span><span class=
"special">::
</span><span class=
"identifier">errorcode
</span> <span class=
"identifier">write_ec
</span><span class=
"special">(
</span> <span class=
"identifier">AsyncAPI
</span> <span class=
"special">&</span> <span class=
"identifier">api
</span><span class=
"special">,
</span> <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">string
</span> <span class=
"keyword">const
</span><span class=
"special">&</span> <span class=
"identifier">data
</span><span class=
"special">)
</span> <span class=
"special">{
</span>
39 <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">fibers
</span><span class=
"special">::
</span><span class=
"identifier">promise
</span><span class=
"special"><</span> <span class=
"identifier">AsyncAPI
</span><span class=
"special">::
</span><span class=
"identifier">errorcode
</span> <span class=
"special">></span> <span class=
"identifier">promise
</span><span class=
"special">;
</span>
40 <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">fibers
</span><span class=
"special">::
</span><span class=
"identifier">future
</span><span class=
"special"><</span> <span class=
"identifier">AsyncAPI
</span><span class=
"special">::
</span><span class=
"identifier">errorcode
</span> <span class=
"special">></span> <span class=
"identifier">future
</span><span class=
"special">(
</span> <span class=
"identifier">promise
</span><span class=
"special">.
</span><span class=
"identifier">get_future
</span><span class=
"special">()
</span> <span class=
"special">);
</span>
41 <span class=
"comment">// In general, even though we block waiting for future::get() and therefore
</span>
42 <span class=
"comment">// won't destroy 'promise' until promise::set_value() has been called, we
</span>
43 <span class=
"comment">// are advised that with threads it's possible for ~promise() to be
</span>
44 <span class=
"comment">// entered before promise::set_value() has returned. While that shouldn't
</span>
45 <span class=
"comment">// happen with fibers::promise, a robust way to deal with the lifespan
</span>
46 <span class=
"comment">// issue is to bind 'promise' into our lambda. Since promise is move-only,
</span>
47 <span class=
"comment">// use initialization capture.
</span>
48 <span class=
"preprocessor">#if
</span> <span class=
"special">!
</span> <span class=
"identifier">defined
</span><span class=
"special">(
</span><span class=
"identifier">BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES
</span><span class=
"special">)
</span>
49 <span class=
"identifier">api
</span><span class=
"special">.
</span><span class=
"identifier">init_write
</span><span class=
"special">(
</span>
50 <span class=
"identifier">data
</span><span class=
"special">,
</span>
51 <span class=
"special">[
</span><span class=
"identifier">promise
</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">promise
</span><span class=
"special">)](
</span> <span class=
"identifier">AsyncAPI
</span><span class=
"special">::
</span><span class=
"identifier">errorcode
</span> <span class=
"identifier">ec
</span><span class=
"special">)
</span> <span class=
"keyword">mutable
</span> <span class=
"special">{
</span>
52 <span class=
"identifier">promise
</span><span class=
"special">.
</span><span class=
"identifier">set_value
</span><span class=
"special">(
</span> <span class=
"identifier">ec
</span><span class=
"special">);
</span>
53 <span class=
"special">});
</span>
55 <span class=
"preprocessor">#else
</span> <span class=
"comment">// defined(BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES)
</span>
56 <span class=
"identifier">api
</span><span class=
"special">.
</span><span class=
"identifier">init_write
</span><span class=
"special">(
</span>
57 <span class=
"identifier">data
</span><span class=
"special">,
</span>
58 <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">bind
</span><span class=
"special">([](
</span><span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">fibers
</span><span class=
"special">::
</span><span class=
"identifier">promise
</span><span class=
"special"><</span> <span class=
"identifier">AsyncAPI
</span><span class=
"special">::
</span><span class=
"identifier">errorcode
</span> <span class=
"special">></span> <span class=
"special">&</span> <span class=
"identifier">promise
</span><span class=
"special">,
</span>
59 <span class=
"identifier">AsyncAPI
</span><span class=
"special">::
</span><span class=
"identifier">errorcode
</span> <span class=
"identifier">ec
</span><span class=
"special">)
</span> <span class=
"special">{
</span>
60 <span class=
"identifier">promise
</span><span class=
"special">.
</span><span class=
"identifier">set_value
</span><span class=
"special">(
</span> <span class=
"identifier">ec
</span><span class=
"special">);
</span>
61 <span class=
"special">},
</span>
62 <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">move
</span><span class=
"special">(
</span> <span class=
"identifier">promise
</span><span class=
"special">),
</span>
63 <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">placeholders
</span><span class=
"special">::
</span><span class=
"identifier">_1
</span><span class=
"special">)
</span> <span class=
"special">);
</span>
64 <span class=
"preprocessor">#endif
</span> <span class=
"comment">// BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES
</span>
66 <span class=
"keyword">return
</span> <span class=
"identifier">future
</span><span class=
"special">.
</span><span class=
"identifier">get
</span><span class=
"special">();
</span>
67 <span class=
"special">}
</span>
74 <div class=
"orderedlist"><ol class=
"orderedlist" type=
"1">
76 Instantiate a
<code class=
"computeroutput"><span class=
"identifier">promise
</span><span class=
"special"><></span></code> of correct type.
79 Obtain its
<code class=
"computeroutput"><span class=
"identifier">future
</span><span class=
"special"><></span></code>.
82 Arrange for the callback to call
<a class=
"link" href=
"../synchronization/futures/promise.html#promise_set_value"><code class=
"computeroutput">promise::set_value()
</code></a>.
85 Block on
<a class=
"link" href=
"../synchronization/futures/future.html#future_get"><code class=
"computeroutput">future::get()
</code></a>.
88 <div class=
"note"><table border=
"0" summary=
"Note">
90 <td rowspan=
"2" align=
"center" valign=
"top" width=
"25"><img alt=
"[Note]" src=
"../../../../../../doc/src/images/note.png"></td>
91 <th align=
"left">Note
</th>
93 <tr><td align=
"left" valign=
"top"><p>
94 This tactic for resuming a pending fiber works even if the callback is
95 called on a different thread than the one on which the initiating fiber
96 is running. In fact,
<a href=
"../../../../examples/adapt_callbacks.cpp" target=
"_top">the
97 example program
’s
</a> dummy
<code class=
"computeroutput"><span class=
"identifier">AsyncAPI
</span></code>
98 implementation illustrates that: it simulates async I/O by launching a
99 new thread that sleeps briefly and then calls the relevant callback.
103 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
104 <td align=
"left"></td>
105 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2013 Oliver Kowalke
<p>
106 Distributed under the Boost Software License, Version
1.0. (See accompanying
107 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>)
112 <div class=
"spirit-nav">
113 <a accesskey=
"p" href=
"overview.html"><img src=
"../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../callbacks.html"><img src=
"../../../../../../doc/src/images/up.png" alt=
"Up"></a><a accesskey=
"h" href=
"../../index.html"><img src=
"../../../../../../doc/src/images/home.png" alt=
"Home"></a><a accesskey=
"n" href=
"success_or_exception.html"><img src=
"../../../../../../doc/src/images/next.png" alt=
"Next"></a>