strand.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>strand</title>
  5. <link rel="stylesheet" href="../../boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
  7. <link rel="home" href="../../index.html" title="Asio">
  8. <link rel="up" href="../reference.html" title="Reference">
  9. <link rel="prev" href="steady_timer.html" title="steady_timer">
  10. <link rel="next" href="stream_socket_service.html" title="stream_socket_service">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr><td valign="top"><img alt="asio C++ library" width="250" height="60" src="../../asio.png"></td></tr></table>
  14. <hr>
  15. <div class="spirit-nav">
  16. <a accesskey="p" href="steady_timer.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u" href="../reference.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="stream_socket_service.html"><img src="../../next.png" alt="Next"></a>
  17. </div>
  18. <div class="section">
  19. <div class="titlepage"><div><div><h3 class="title">
  20. <a name="asio.reference.strand"></a><a class="link" href="strand.html" title="strand">strand</a>
  21. </h3></div></div></div>
  22. <p>
  23. <a class="indexterm" name="idp160421088"></a>
  24. Typedef for backwards compatibility.
  25. </p>
  26. <pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">strand</span> <span class="identifier">strand</span><span class="special">;</span>
  27. </pre>
  28. <h5>
  29. <a name="asio.reference.strand.h0"></a>
  30. <span><a name="asio.reference.strand.member_functions"></a></span><a class="link" href="strand.html#asio.reference.strand.member_functions">Member
  31. Functions</a>
  32. </h5>
  33. <div class="informaltable"><table class="table">
  34. <colgroup>
  35. <col>
  36. <col>
  37. </colgroup>
  38. <thead><tr>
  39. <th>
  40. <p>
  41. Name
  42. </p>
  43. </th>
  44. <th>
  45. <p>
  46. Description
  47. </p>
  48. </th>
  49. </tr></thead>
  50. <tbody>
  51. <tr>
  52. <td>
  53. <p>
  54. <a class="link" href="io_service__strand/dispatch.html" title="io_service::strand::dispatch"><span class="bold"><strong>dispatch</strong></span></a>
  55. </p>
  56. </td>
  57. <td>
  58. <p>
  59. Request the strand to invoke the given handler.
  60. </p>
  61. </td>
  62. </tr>
  63. <tr>
  64. <td>
  65. <p>
  66. <a class="link" href="io_service__strand/get_io_service.html" title="io_service::strand::get_io_service"><span class="bold"><strong>get_io_service</strong></span></a>
  67. </p>
  68. </td>
  69. <td>
  70. <p>
  71. Get the io_service associated with the strand.
  72. </p>
  73. </td>
  74. </tr>
  75. <tr>
  76. <td>
  77. <p>
  78. <a class="link" href="io_service__strand/post.html" title="io_service::strand::post"><span class="bold"><strong>post</strong></span></a>
  79. </p>
  80. </td>
  81. <td>
  82. <p>
  83. Request the strand to invoke the given handler and return immediately.
  84. </p>
  85. </td>
  86. </tr>
  87. <tr>
  88. <td>
  89. <p>
  90. <a class="link" href="io_service__strand/running_in_this_thread.html" title="io_service::strand::running_in_this_thread"><span class="bold"><strong>running_in_this_thread</strong></span></a>
  91. </p>
  92. </td>
  93. <td>
  94. <p>
  95. Determine whether the strand is running in the current thread.
  96. </p>
  97. </td>
  98. </tr>
  99. <tr>
  100. <td>
  101. <p>
  102. <a class="link" href="io_service__strand/strand.html" title="io_service::strand::strand"><span class="bold"><strong>strand</strong></span></a>
  103. </p>
  104. </td>
  105. <td>
  106. <p>
  107. Constructor.
  108. </p>
  109. </td>
  110. </tr>
  111. <tr>
  112. <td>
  113. <p>
  114. <a class="link" href="io_service__strand/wrap.html" title="io_service::strand::wrap"><span class="bold"><strong>wrap</strong></span></a>
  115. </p>
  116. </td>
  117. <td>
  118. <p>
  119. Create a new handler that automatically dispatches the wrapped
  120. handler on the strand.
  121. </p>
  122. </td>
  123. </tr>
  124. <tr>
  125. <td>
  126. <p>
  127. <a class="link" href="io_service__strand/_strand.html" title="io_service::strand::~strand"><span class="bold"><strong>~strand</strong></span></a>
  128. </p>
  129. </td>
  130. <td>
  131. <p>
  132. Destructor.
  133. </p>
  134. </td>
  135. </tr>
  136. </tbody>
  137. </table></div>
  138. <p>
  139. The <a class="link" href="io_service__strand.html" title="io_service::strand"><code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">strand</span></code></a>
  140. class provides the ability to post and dispatch handlers with the guarantee
  141. that none of those handlers will execute concurrently.
  142. </p>
  143. <h5>
  144. <a name="asio.reference.strand.h1"></a>
  145. <span><a name="asio.reference.strand.order_of_handler_invocation"></a></span><a class="link" href="strand.html#asio.reference.strand.order_of_handler_invocation">Order
  146. of handler invocation</a>
  147. </h5>
  148. <p>
  149. Given:
  150. </p>
  151. <div class="itemizedlist"><ul class="itemizedlist" type="disc">
  152. <li class="listitem">
  153. a strand object <code class="computeroutput"><span class="identifier">s</span></code>
  154. </li>
  155. <li class="listitem">
  156. an object <code class="computeroutput"><span class="identifier">a</span></code> meeting completion
  157. handler requirements
  158. </li>
  159. <li class="listitem">
  160. an object <code class="computeroutput"><span class="identifier">a1</span></code> which is
  161. an arbitrary copy of <code class="computeroutput"><span class="identifier">a</span></code>
  162. made by the implementation
  163. </li>
  164. <li class="listitem">
  165. an object <code class="computeroutput"><span class="identifier">b</span></code> meeting completion
  166. handler requirements
  167. </li>
  168. <li class="listitem">
  169. an object <code class="computeroutput"><span class="identifier">b1</span></code> which is
  170. an arbitrary copy of <code class="computeroutput"><span class="identifier">b</span></code>
  171. made by the implementation
  172. </li>
  173. </ul></div>
  174. <p>
  175. if any of the following conditions are true:
  176. </p>
  177. <div class="itemizedlist"><ul class="itemizedlist" type="disc">
  178. <li class="listitem">
  179. <code class="computeroutput"><span class="identifier">s</span><span class="special">.</span><span class="identifier">post</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</span></code>
  180. happens-before <code class="computeroutput"><span class="identifier">s</span><span class="special">.</span><span class="identifier">post</span><span class="special">(</span><span class="identifier">b</span><span class="special">)</span></code>
  181. </li>
  182. <li class="listitem">
  183. <code class="computeroutput"><span class="identifier">s</span><span class="special">.</span><span class="identifier">post</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</span></code>
  184. happens-before <code class="computeroutput"><span class="identifier">s</span><span class="special">.</span><span class="identifier">dispatch</span><span class="special">(</span><span class="identifier">b</span><span class="special">)</span></code>,
  185. where the latter is performed outside the strand
  186. </li>
  187. <li class="listitem">
  188. <code class="computeroutput"><span class="identifier">s</span><span class="special">.</span><span class="identifier">dispatch</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</span></code>
  189. happens-before <code class="computeroutput"><span class="identifier">s</span><span class="special">.</span><span class="identifier">post</span><span class="special">(</span><span class="identifier">b</span><span class="special">)</span></code>,
  190. where the former is performed outside the strand
  191. </li>
  192. <li class="listitem">
  193. <code class="computeroutput"><span class="identifier">s</span><span class="special">.</span><span class="identifier">dispatch</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</span></code>
  194. happens-before <code class="computeroutput"><span class="identifier">s</span><span class="special">.</span><span class="identifier">dispatch</span><span class="special">(</span><span class="identifier">b</span><span class="special">)</span></code>,
  195. where both are performed outside the strand
  196. </li>
  197. </ul></div>
  198. <p>
  199. then <code class="computeroutput"><span class="identifier">asio_handler_invoke</span><span class="special">(</span><span class="identifier">a1</span><span class="special">,</span>
  200. <span class="special">&amp;</span><span class="identifier">a1</span><span class="special">)</span></code> happens-before <code class="computeroutput"><span class="identifier">asio_handler_invoke</span><span class="special">(</span><span class="identifier">b1</span><span class="special">,</span>
  201. <span class="special">&amp;</span><span class="identifier">b1</span><span class="special">)</span></code>.
  202. </p>
  203. <p>
  204. Note that in the following case:
  205. </p>
  206. <pre class="programlisting"><span class="identifier">async_op_1</span><span class="special">(...,</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">wrap</span><span class="special">(</span><span class="identifier">a</span><span class="special">));</span>
  207. <span class="identifier">async_op_2</span><span class="special">(...,</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">wrap</span><span class="special">(</span><span class="identifier">b</span><span class="special">));</span>
  208. </pre>
  209. <p>
  210. the completion of the first async operation will perform <code class="computeroutput"><span class="identifier">s</span><span class="special">.</span><span class="identifier">dispatch</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</span></code>,
  211. and the second will perform <code class="computeroutput"><span class="identifier">s</span><span class="special">.</span><span class="identifier">dispatch</span><span class="special">(</span><span class="identifier">b</span><span class="special">)</span></code>,
  212. but the order in which those are performed is unspecified. That is, you cannot
  213. state whether one happens-before the other. Therefore none of the above conditions
  214. are met and no ordering guarantee is made.
  215. </p>
  216. <h5>
  217. <a name="asio.reference.strand.h2"></a>
  218. <span><a name="asio.reference.strand.remarks"></a></span><a class="link" href="strand.html#asio.reference.strand.remarks">Remarks</a>
  219. </h5>
  220. <p>
  221. The implementation makes no guarantee that handlers posted or dispatched
  222. through different <code class="computeroutput"><span class="identifier">strand</span></code>
  223. objects will be invoked concurrently.
  224. </p>
  225. <h5>
  226. <a name="asio.reference.strand.h3"></a>
  227. <span><a name="asio.reference.strand.thread_safety"></a></span><a class="link" href="strand.html#asio.reference.strand.thread_safety">Thread
  228. Safety</a>
  229. </h5>
  230. <p>
  231. <span class="emphasis"><em>Distinct</em></span> <span class="emphasis"><em>objects:</em></span> Safe.
  232. </p>
  233. <p>
  234. <span class="emphasis"><em>Shared</em></span> <span class="emphasis"><em>objects:</em></span> Safe.
  235. </p>
  236. <h5>
  237. <a name="asio.reference.strand.h4"></a>
  238. <span><a name="asio.reference.strand.requirements"></a></span><a class="link" href="strand.html#asio.reference.strand.requirements">Requirements</a>
  239. </h5>
  240. <p>
  241. <span class="emphasis"><em>Header: </em></span><code class="literal">asio/strand.hpp</code>
  242. </p>
  243. <p>
  244. <span class="emphasis"><em>Convenience header: </em></span><code class="literal">asio.hpp</code>
  245. </p>
  246. </div>
  247. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  248. <td align="left"></td>
  249. <td align="right"><div class="copyright-footer">Copyright &#169; 2003-2014 Christopher M. Kohlhoff<p>
  250. Distributed under the Boost Software License, Version 1.0. (See accompanying
  251. 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>)
  252. </p>
  253. </div></td>
  254. </tr></table>
  255. <hr>
  256. <div class="spirit-nav">
  257. <a accesskey="p" href="steady_timer.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u" href="../reference.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="stream_socket_service.html"><img src="../../next.png" alt="Next"></a>
  258. </div>
  259. </body>
  260. </html>