1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Concurrency Hints</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="../core.html" title="Core Concepts and Functionality"> 9<link rel="prev" href="handler_tracking.html" title="Handler Tracking"> 10<link rel="next" href="coroutine.html" title="Stackless Coroutines"> 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="handler_tracking.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="coroutine.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.core.concurrency_hint"></a><a class="link" href="concurrency_hint.html" title="Concurrency Hints">Concurrency 28 Hints</a> 29</h4></div></div></div> 30<p> 31 The <a class="link" href="../../reference/io_context/io_context.html" title="io_context::io_context"><code class="computeroutput"><span class="identifier">io_context</span></code> constructor</a> allows 32 programs to specify a concurrency hint. This is a suggestion to the <code class="computeroutput"><span class="identifier">io_context</span></code> implementation as to the number 33 of active threads that should be used for running completion handlers. 34 </p> 35<p> 36 When the Windows I/O completion port backend is in use, this value is passed 37 to <code class="literal">CreateIoCompletionPort</code>. 38 </p> 39<p> 40 When a reactor-based backend is used, the implementation recognises the 41 following special concurrency hint values: 42 </p> 43<div class="informaltable"><table class="table"> 44<colgroup> 45<col> 46<col> 47</colgroup> 48<thead><tr> 49<th> 50 <p> 51 Value 52 </p> 53 </th> 54<th> 55 <p> 56 Description 57 </p> 58 </th> 59</tr></thead> 60<tbody> 61<tr> 62<td> 63 <p> 64 <code class="computeroutput"><span class="number">1</span></code> 65 </p> 66 </td> 67<td> 68 <p> 69 The implementation assumes that the <code class="computeroutput"><span class="identifier">io_context</span></code> 70 will be run from a single thread, and applies several optimisations 71 based on this assumption. 72 </p> 73 <p> 74 For example, when a handler is posted from within another handler, 75 the new handler is added to a fast thread-local queue (with the 76 consequence that the new handler is held back until the currently 77 executing handler finishes). 78 </p> 79 </td> 80</tr> 81<tr> 82<td> 83 <p> 84 <code class="computeroutput"><span class="identifier">BOOST_ASIO_CONCURRENCY_HINT_UNSAFE</span></code> 85 </p> 86 </td> 87<td> 88 <p> 89 This special concurrency hint disables locking in both the scheduler 90 and reactor I/O. This hint has the following restrictions: 91 </p> 92 <p> 93 — Care must be taken to ensure that all operations on the <code class="computeroutput"><span class="identifier">io_context</span></code> and any of its associated 94 I/O objects (such as sockets and timers) occur in only one thread 95 at a time. 96 </p> 97 <p> 98 — Asynchronous resolve operations fail with <code class="computeroutput"><span class="identifier">operation_not_supported</span></code>. 99 </p> 100 <p> 101 — If a <code class="computeroutput"><span class="identifier">signal_set</span></code> 102 is used with the <code class="computeroutput"><span class="identifier">io_context</span></code>, 103 <code class="computeroutput"><span class="identifier">signal_set</span></code> objects 104 cannot be used with any other io_context in the program. 105 </p> 106 </td> 107</tr> 108<tr> 109<td> 110 <p> 111 <code class="computeroutput"><span class="identifier">BOOST_ASIO_CONCURRENCY_HINT_UNSAFE_IO</span></code> 112 </p> 113 </td> 114<td> 115 <p> 116 This special concurrency hint disables locking in the reactor 117 I/O. This hint has the following restrictions: 118 </p> 119 <p> 120 — Care must be taken to ensure that run functions on the <code class="computeroutput"><span class="identifier">io_context</span></code>, and all operations 121 on the context's associated I/O objects (such as sockets and 122 timers), occur in only one thread at a time. 123 </p> 124 </td> 125</tr> 126<tr> 127<td> 128 <p> 129 <code class="computeroutput"><span class="identifier">BOOST_ASIO_CONCURRENCY_HINT_SAFE</span></code> 130 </p> 131 </td> 132<td> 133 <p> 134 The default. The <code class="computeroutput"><span class="identifier">io_context</span></code> 135 provides full thread safety, and distinct I/O objects may be 136 used from any thread. 137 </p> 138 </td> 139</tr> 140</tbody> 141</table></div> 142<p> 143 The concurrency hint used by default-constructed <code class="computeroutput">io_context</code> 144 objects can be overridden at compile time by defining the <code class="computeroutput">BOOST_ASIO_CONCURRENCY_HINT_DEFAULT</code> 145 macro. For example, specifying 146 </p> 147<pre class="programlisting">-DBOOST_ASIO_CONCURRENCY_HINT_DEFAULT=1 148</pre> 149<p> 150 on the compiler command line means that a concurrency hint of <code class="computeroutput">1</code> 151 is used for all default-constructed <code class="computeroutput">io_context</code> objects in 152 the program. Similarly, the concurrency hint used by <code class="computeroutput">io_context</code> 153 objects constructed with <code class="computeroutput">1</code> can be overridden by defining 154 <code class="computeroutput">BOOST_ASIO_CONCURRENCY_HINT_1</code>. For example, passing 155 </p> 156<pre class="programlisting">-DBOOST_ASIO_CONCURRENCY_HINT_1=BOOST_ASIO_CONCURRENCY_HINT_UNSAFE 157</pre> 158<p> 159 to the compiler will disable thread safety for all of these objects. 160 </p> 161</div> 162<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 163<td align="left"></td> 164<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 165 Kohlhoff<p> 166 Distributed under the Boost Software License, Version 1.0. (See accompanying 167 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>) 168 </p> 169</div></td> 170</tr></table> 171<hr> 172<div class="spirit-nav"> 173<a accesskey="p" href="handler_tracking.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="coroutine.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 174</div> 175</body> 176</html> 177