3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Overview
</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=
"../callbacks.html" title=
"Integrating Fibers with Asynchronous Callbacks">
10 <link rel=
"next" href=
"return_errorcode.html" title=
"Return Errorcode">
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=
"../callbacks.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=
"return_errorcode.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.overview"></a><a class=
"link" href=
"overview.html" title=
"Overview">Overview
</a>
28 </h3></div></div></div>
30 One of the primary benefits of
<span class=
"bold"><strong>Boost.Fiber
</strong></span>
31 is the ability to use asynchronous operations for efficiency, while at the
32 same time structuring the calling code
<span class=
"emphasis"><em>as if
</em></span> the operations
33 were synchronous. Asynchronous operations provide completion notification
34 in a variety of ways, but most involve a callback function of some kind.
35 This section discusses tactics for interfacing
<span class=
"bold"><strong>Boost.Fiber
</strong></span>
36 with an arbitrary async operation.
39 For purposes of illustration, consider the following hypothetical API:
43 <pre class=
"programlisting"><span class=
"keyword">class
</span> <span class=
"identifier">AsyncAPI
</span> <span class=
"special">{
</span>
44 <span class=
"keyword">public
</span><span class=
"special">:
</span>
45 <span class=
"comment">// constructor acquires some resource that can be read and written
</span>
46 <span class=
"identifier">AsyncAPI
</span><span class=
"special">();
</span>
48 <span class=
"comment">// callbacks accept an int error code;
0 == success
</span>
49 <span class=
"keyword">typedef
</span> <span class=
"keyword">int
</span> <span class=
"identifier">errorcode
</span><span class=
"special">;
</span>
51 <span class=
"comment">// write callback only needs to indicate success or failure
</span>
52 <span class=
"keyword">template
</span><span class=
"special"><</span> <span class=
"keyword">typename
</span> <span class=
"identifier">Fn
</span> <span class=
"special">></span>
53 <span class=
"keyword">void
</span> <span class=
"identifier">init_write
</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=
"identifier">Fn
</span> <span class=
"special">&&</span> <span class=
"identifier">callback
</span><span class=
"special">);
</span>
55 <span class=
"comment">// read callback needs to accept both errorcode and data
</span>
56 <span class=
"keyword">template
</span><span class=
"special"><</span> <span class=
"keyword">typename
</span> <span class=
"identifier">Fn
</span> <span class=
"special">></span>
57 <span class=
"keyword">void
</span> <span class=
"identifier">init_read
</span><span class=
"special">(
</span> <span class=
"identifier">Fn
</span> <span class=
"special">&&</span> <span class=
"identifier">callback
</span><span class=
"special">);
</span>
59 <span class=
"comment">// ... other operations ...
</span>
60 <span class=
"special">};
</span>
65 The significant points about each of
<code class=
"computeroutput"><span class=
"identifier">init_write
</span><span class=
"special">()
</span></code> and
<code class=
"computeroutput"><span class=
"identifier">init_read
</span><span class=
"special">()
</span></code> are:
67 <div class=
"itemizedlist"><ul class=
"itemizedlist" type=
"disc">
69 The
<code class=
"computeroutput"><span class=
"identifier">AsyncAPI
</span></code> method only
70 initiates the operation. It returns immediately, while the requested
71 operation is still pending.
74 The method accepts a callback. When the operation completes, the callback
75 is called with relevant parameters (error code, data if applicable).
79 We would like to wrap these asynchronous methods in functions that appear
80 synchronous by blocking the calling fiber until the operation completes.
81 This lets us use the wrapper function
’s return value to deliver relevant data.
83 <div class=
"tip"><table border=
"0" summary=
"Tip">
85 <td rowspan=
"2" align=
"center" valign=
"top" width=
"25"><img alt=
"[Tip]" src=
"../../../../../../doc/src/images/tip.png"></td>
86 <th align=
"left">Tip
</th>
88 <tr><td align=
"left" valign=
"top"><p>
89 <a class=
"link" href=
"../synchronization/futures/promise.html#class_promise"><code class=
"computeroutput">promise
<></code></a> and
<a class=
"link" href=
"../synchronization/futures/future.html#class_future"><code class=
"computeroutput">future
<></code></a> are your friends
94 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
95 <td align=
"left"></td>
96 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2013 Oliver Kowalke
<p>
97 Distributed under the Boost Software License, Version
1.0. (See accompanying
98 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>)
103 <div class=
"spirit-nav">
104 <a accesskey=
"p" href=
"../callbacks.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=
"return_errorcode.html"><img src=
"../../../../../../doc/src/images/next.png" alt=
"Next"></a>