3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Strands: Use Threads Without Explicit Locking</title>
5 <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
7 <link rel="home" href="../../../boost_asio.html" title="Boost.Asio">
8 <link rel="up" href="../core.html" title="Core Concepts and Functionality">
9 <link rel="prev" href="threads.html" title="Threads and Boost.Asio">
10 <link rel="next" href="buffers.html" title="Buffers">
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="threads.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../core.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="buffers.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h4 class="title">
27 <a name="boost_asio.overview.core.strands"></a><a class="link" href="strands.html" title="Strands: Use Threads Without Explicit Locking">Strands: Use Threads
28 Without Explicit Locking</a>
29 </h4></div></div></div>
31 A strand is defined as a strictly sequential invocation of event handlers
32 (i.e. no concurrent invocation). Use of strands allows execution of code
33 in a multithreaded program without the need for explicit locking (e.g.
37 Strands may be either implicit or explicit, as illustrated by the following
38 alternative approaches:
40 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
42 Calling io_service::run() from only one thread means all event handlers
43 execute in an implicit strand, due to the io_service's guarantee that
44 handlers are only invoked from inside run().
47 Where there is a single chain of asynchronous operations associated
48 with a connection (e.g. in a half duplex protocol implementation like
49 HTTP) there is no possibility of concurrent execution of the handlers.
50 This is an implicit strand.
53 An explicit strand is an instance of <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">strand</span></code>.
54 All event handler function objects need to be wrapped using <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">strand</span><span class="special">::</span><span class="identifier">wrap</span><span class="special">()</span></code>
55 or otherwise posted/dispatched through the <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">strand</span></code>
60 In the case of composed asynchronous operations, such as <code class="computeroutput"><span class="identifier">async_read</span><span class="special">()</span></code>
61 or <code class="computeroutput"><span class="identifier">async_read_until</span><span class="special">()</span></code>,
62 if a completion handler goes through a strand, then all intermediate handlers
63 should also go through the same strand. This is needed to ensure thread
64 safe access for any objects that are shared between the caller and the
65 composed operation (in the case of <code class="computeroutput"><span class="identifier">async_read</span><span class="special">()</span></code> it's the socket, which the caller can
66 close() to cancel the operation). This is done by having hook functions
67 for all intermediate handlers which forward the calls to the customisable
68 hook associated with the final handler:
70 <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">my_handler</span>
71 <span class="special">{</span>
72 <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()()</span> <span class="special">{</span> <span class="special">...</span> <span class="special">}</span>
73 <span class="special">};</span>
75 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">></span>
76 <span class="keyword">void</span> <span class="identifier">asio_handler_invoke</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">my_handler</span><span class="special">*)</span>
77 <span class="special">{</span>
78 <span class="comment">// Do custom invocation here.</span>
79 <span class="comment">// Default implementation calls f();</span>
80 <span class="special">}</span>
83 The <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">strand</span><span class="special">::</span><span class="identifier">wrap</span><span class="special">()</span></code>
84 function creates a new completion handler that defines <code class="computeroutput"><span class="identifier">asio_handler_invoke</span></code>
85 so that the function object is executed through the strand.
88 <a name="boost_asio.overview.core.strands.h0"></a>
89 <span class="phrase"><a name="boost_asio.overview.core.strands.see_also"></a></span><a class="link" href="strands.html#boost_asio.overview.core.strands.see_also">See
93 <a class="link" href="../../reference/io_service__strand.html" title="io_service::strand">io_service::strand</a>,
94 <a class="link" href="../../tutorial/tuttimer5.html" title="Timer.5 - Synchronising handlers in multithreaded programs">tutorial Timer.5</a>,
95 <a class="link" href="../../examples/cpp03_examples.html#boost_asio.examples.cpp03_examples.http_server_3">HTTP server
99 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
100 <td align="left"></td>
101 <td align="right"><div class="copyright-footer">Copyright © 2003-2014 Christopher M. Kohlhoff<p>
102 Distributed under the Boost Software License, Version 1.0. (See accompanying
103 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>)
108 <div class="spirit-nav">
109 <a accesskey="p" href="threads.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../core.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="buffers.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>