• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Proposed Standard Executors</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="../boost_asio.html" title="Boost.Asio">
9<link rel="prev" href="net_ts.html" title="Networking TS compatibility">
10<link rel="next" href="history.html" title="Revision History">
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="net_ts.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_asio.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="history.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h2 class="title" style="clear: both">
27<a name="boost_asio.std_executors"></a><a class="link" href="std_executors.html" title="Proposed Standard Executors">Proposed Standard Executors</a>
28</h2></div></div></div>
29<p>
30      Boost.Asio provides a complete implementation of the proposed standard executors,
31      as described in <a href="http://wg21.link/P0443r13" target="_top">P0443r13</a>, <a href="http://wg21.link/P1348r0" target="_top">P1348r0</a>, and <a href="http://wg21.link/P1393r0" target="_top">P1393r0</a>.
32    </p>
33<p>
34      Just as with executors under the Networking TS model, a standard executor represents
35      a policy as to how, when, and where a piece of code should be executed. Most
36      existing code should continue to work with little or no change.
37    </p>
38<h4>
39<a name="boost_asio.std_executors.h0"></a>
40      <span class="phrase"><a name="boost_asio.std_executors.standard_executor_implementations_in_boost_asio"></a></span><a class="link" href="std_executors.html#boost_asio.std_executors.standard_executor_implementations_in_boost_asio">Standard
41      Executor Implementations in Boost.Asio</a>
42    </h4>
43<p>
44      The <a class="link" href="reference/io_context/executor_type.html" title="io_context::executor_type"><code class="computeroutput">io_context::executor_type</code></a>,
45      <a class="link" href="reference/thread_pool/executor_type.html" title="thread_pool::executor_type"><code class="computeroutput">thread_pool::executor_type</code></a>,
46      <a class="link" href="reference/system_executor.html" title="system_executor"><code class="computeroutput">system_executor</code></a>,
47      and <a class="link" href="reference/strand.html" title="strand"><code class="computeroutput">strand</code></a>
48      executors meet the requirements for the proposed standard executors. For compatibility,
49      these classes also meet the requirements for the Networking TS model of executors.
50    </p>
51<h4>
52<a name="boost_asio.std_executors.h1"></a>
53      <span class="phrase"><a name="boost_asio.std_executors.standard_executor_use_in_boost_asio"></a></span><a class="link" href="std_executors.html#boost_asio.std_executors.standard_executor_use_in_boost_asio">Standard
54      Executor Use in Boost.Asio</a>
55    </h4>
56<p>
57      All I/O objects such as <a class="link" href="reference/ip__tcp/socket.html" title="ip::tcp::socket"><code class="computeroutput">ip::tcp::socket</code></a>,
58      asynchronous operations, and utilities including <a class="link" href="reference/dispatch.html" title="dispatch"><code class="computeroutput">dispatch</code></a>,
59      <a class="link" href="reference/post.html" title="post"><code class="computeroutput">post</code></a>, <a class="link" href="reference/defer.html" title="defer"><code class="computeroutput">defer</code></a>,
60      <a class="link" href="reference/get_associated_executor.html" title="get_associated_executor"><code class="computeroutput">get_associated_executor</code></a>,
61      <a class="link" href="reference/bind_executor.html" title="bind_executor"><code class="computeroutput">bind_executor</code></a>,
62      <a class="link" href="reference/make_work_guard.html" title="make_work_guard"><code class="computeroutput">make_work_guard</code></a>,
63      <a class="link" href="reference/spawn.html" title="spawn"><code class="computeroutput">spawn</code></a>, <a class="link" href="reference/co_spawn.html" title="co_spawn"><code class="computeroutput">co_spawn</code></a>, <a class="link" href="reference/async_compose.html" title="async_compose"><code class="computeroutput">async_compose</code></a>,
64      <a class="link" href="reference/use_future.html" title="use_future"><code class="computeroutput">use_future</code></a>,
65      etc., can interoperate with both proposed standard executors, and with Networking
66      TS executors. Boost.Asio's implementation determines at compile time which
67      model a particular executor meets; the proposed standard executor model is
68      used in preference if both are detected.
69    </p>
70<p>
71      Support for the existing Networking TS model of executors can be disabled by
72      defining <code class="computeroutput">BOOST_ASIO_NO_TS_EXECUTORS</code>.
73    </p>
74<h4>
75<a name="boost_asio.std_executors.h2"></a>
76      <span class="phrase"><a name="boost_asio.std_executors.polymorphic_i_o_executor"></a></span><a class="link" href="std_executors.html#boost_asio.std_executors.polymorphic_i_o_executor">Polymorphic
77      I/O Executor</a>
78    </h4>
79<p>
80      The <a class="link" href="reference/any_io_executor.html" title="any_io_executor"><code class="computeroutput">any_io_executor</code></a>
81      type alias is the default runtime-polymorphic executor for all I/O objects.
82      This type alias points to the <a class="link" href="reference/execution__any_executor.html" title="execution::any_executor"><code class="computeroutput">execution::any_executor&lt;&gt;</code></a>
83      template with a set of supportable properties specified for use with I/O.
84    </p>
85<p>
86      This new name may break existing code that directly uses the old polymorphic
87      wrapper, <a class="link" href="reference/executor.html" title="executor"><code class="computeroutput">executor</code></a>.
88      If required for backward compatibility, <code class="computeroutput">BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT</code>
89      can be defined, which changes the <code class="computeroutput">any_io_executor</code> type alias to
90      instead point to the <code class="computeroutput">executor</code> polymorphic wrapper.
91    </p>
92<h4>
93<a name="boost_asio.std_executors.h3"></a>
94      <span class="phrase"><a name="boost_asio.std_executors.implementing_a_minimal_i_o_executor"></a></span><a class="link" href="std_executors.html#boost_asio.std_executors.implementing_a_minimal_i_o_executor">Implementing
95      a Minimal I/O Executor</a>
96    </h4>
97<p>
98      Standard executor properties make what were previously hard requirements on
99      an executor (such as work counting, or the ability to distinguish between
100      <code class="computeroutput">post</code>, <code class="computeroutput">dispatch</code>, and <code class="computeroutput">defer</code>) into optional
101      facilities. With this relaxation, the minimal requirements for an I/O executor
102      are:
103    </p>
104<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
105<li class="listitem">
106          Conformance to the <a class="link" href="reference/Executor1.html#boost_asio.reference.Executor1.standard_executors"><code class="computeroutput">executor</code></a>
107          concept.
108        </li>
109<li class="listitem">
110          The ability to query the <a class="link" href="reference/execution__context.html" title="execution::context"><code class="computeroutput">execution::context</code></a>
111          property, with the result being <a class="link" href="reference/execution_context.html" title="execution_context"><code class="computeroutput">execution_context&amp;</code></a>
112          or a reference to a class that is derived from <code class="computeroutput">execution_context</code>.
113        </li>
114<li class="listitem">
115          The <code class="computeroutput">execute</code> operation having, at minimum, the <a class="link" href="reference/execution__blocking_t/never.html" title="execution::blocking_t::never"><code class="computeroutput">execution::blocking.never</code></a>
116          semantic.
117        </li>
118</ul></div>
119<p>
120      The following example shows a minimal I/O executor. Given a queue submission
121      operation implemented elsewhere:
122    </p>
123<p>
124</p>
125<pre class="programlisting">queue_t queue_create();
126template &lt;typename F&gt; void queue_submit(queue_t q, F f);
127</pre>
128<p>
129    </p>
130<p>
131      the executor may be defined as follows:
132    </p>
133<p>
134</p>
135<pre class="programlisting">struct minimal_io_executor
136{
137  boost::asio::execution_context* context_;
138  queue_t queue_;
139
140  bool operator==(const minimal_io_executor&amp; other) const noexcept
141  {
142    return context_ == other.context_ &amp;&amp; queue_ == other.queue_;
143  }
144
145  bool operator!=(const minimal_io_executor&amp; other) const noexcept
146  {
147    return !(*this == other);
148  }
149
150  boost::asio::execution_context&amp; query(
151      boost::asio::execution::context_t) const noexcept
152  {
153    return *context_;
154  }
155
156  static constexpr boost::asio::execution::blocking_t::never_t query(
157      boost::asio::execution::blocking_t) noexcept
158  {
159    // This executor always has blocking.never semantics.
160    return boost::asio::execution::blocking.never;
161  }
162
163  template &lt;class F&gt;
164  void execute(F f) const
165  {
166    queue_submit(queue_, std::move(f));
167  }
168};
169</pre>
170<p>
171    </p>
172<p>
173      This executor may be created as follows:
174    </p>
175<p>
176</p>
177<pre class="programlisting">boost::asio::execution_context context;
178queue_t queue = queue_create();
179minimal_io_executor executor{&amp;context, queue};
180</pre>
181<p>
182    </p>
183<p>
184      and then used with I/O objects:
185    </p>
186<p>
187</p>
188<pre class="programlisting">boost::asio::ip::tcp::acceptor acceptor(executor);
189</pre>
190<p>
191    </p>
192<p>
193      or assigned into the <a class="link" href="reference/any_io_executor.html" title="any_io_executor"><code class="computeroutput">any_io_executor</code></a>
194      polymorphic wrapper:
195    </p>
196<p>
197</p>
198<pre class="programlisting">boost::asio::any_io_executor poly_executor = executor;
199</pre>
200<p>
201    </p>
202<h4>
203<a name="boost_asio.std_executors.h4"></a>
204      <span class="phrase"><a name="boost_asio.std_executors.traits_for_deducing_conformance_to_the_executor_concept"></a></span><a class="link" href="std_executors.html#boost_asio.std_executors.traits_for_deducing_conformance_to_the_executor_concept">Traits
205      for Deducing Conformance to the Executor Concept</a>
206    </h4>
207<p>
208      Older C++ standards and compilers require some assistance to determine whether
209      an executor implementation conforms to the <code class="computeroutput">executor</code> concept and
210      type requirements. This is achieved through specialisation of traits. The following
211      code shows a specialisation of these traits for the <code class="computeroutput">minimal_io_executor</code>
212      example from above:
213    </p>
214<p>
215</p>
216<pre class="programlisting">namespace boost { namespace asio {
217namespace traits {
218
219#if !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
220
221template &lt;typename F&gt;
222struct execute_member&lt;minimal_io_executor, F&gt;
223{
224  static constexpr bool is_valid = true;
225  static constexpr bool is_noexcept = true;
226  typedef void result_type;
227};
228
229#endif // !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
230#if !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
231
232template &lt;&gt;
233struct equality_comparable&lt;minimal_io_executor&gt;
234{
235  static constexpr bool is_valid = true;
236  static constexpr bool is_noexcept = true;
237};
238
239#endif // !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
240#if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
241
242template &lt;&gt;
243struct query_member&lt;minimal_io_executor,
244    boost::asio::execution::context_t&gt;
245{
246  static constexpr bool is_valid = true;
247  static constexpr bool is_noexcept = true;
248  typedef boost::asio::execution_context&amp; result_type;
249};
250
251#endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
252#if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
253
254template &lt;typename Property&gt;
255struct query_static_constexpr_member&lt;minimal_io_executor, Property,
256    typename enable_if&lt;
257      std::is_convertible&lt;Property, boost::asio::execution::blocking_t&gt;::value
258    &gt;::type&gt;
259{
260  static constexpr bool is_valid = true;
261  static constexpr bool is_noexcept = true;
262  typedef boost::asio::execution::blocking_t::never_t result_type;
263  static constexpr result_type value() noexcept { return result_type(); }
264};
265
266#endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
267
268} // namespace traits
269} } // namespace boost::asio
270</pre>
271<p>
272    </p>
273<p>
274      Boost.Asio uses an extensive set of traits to implement all of the proposed
275      standard executor functionality on older C++ standards. These traits may be
276      found under the <code class="literal">boost/asio/traits</code> include directory.
277    </p>
278</div>
279<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
280<td align="left"></td>
281<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
282      Kohlhoff<p>
283        Distributed under the Boost Software License, Version 1.0. (See accompanying
284        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>)
285      </p>
286</div></td>
287</tr></table>
288<hr>
289<div class="spirit-nav">
290<a accesskey="p" href="net_ts.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_asio.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="history.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
291</div>
292</body>
293</html>
294