• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>io_service</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="../reference.html" title="Reference">
9<link rel="prev" href="io_context__work/_work.html" title="io_context::work::~work">
10<link rel="next" href="ip__address.html" title="ip::address">
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="io_context__work/_work.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.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="ip__address.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="boost_asio.reference.io_service"></a><a class="link" href="io_service.html" title="io_service">io_service</a>
28</h3></div></div></div>
29<p>
30        <a class="indexterm" name="boost_asio.indexterm.io_service"></a>
31Typedef for backwards compatibility.
32      </p>
33<pre class="programlisting">typedef io_context io_service;
34</pre>
35<h5>
36<a name="boost_asio.reference.io_service.h0"></a>
37        <span class="phrase"><a name="boost_asio.reference.io_service.types"></a></span><a class="link" href="io_service.html#boost_asio.reference.io_service.types">Types</a>
38      </h5>
39<div class="informaltable"><table class="table">
40<colgroup>
41<col>
42<col>
43</colgroup>
44<thead><tr>
45<th>
46                <p>
47                  Name
48                </p>
49              </th>
50<th>
51                <p>
52                  Description
53                </p>
54              </th>
55</tr></thead>
56<tbody>
57<tr>
58<td>
59                <p>
60                  <a class="link" href="io_context__basic_executor_type.html" title="io_context::basic_executor_type"><span class="bold"><strong>basic_executor_type</strong></span></a>
61                </p>
62              </td>
63<td>
64                <p>
65                  Executor implementation type used to submit functions to an io_context.
66                </p>
67              </td>
68</tr>
69<tr>
70<td>
71                <p>
72                  <a class="link" href="io_context__service.html" title="io_context::service"><span class="bold"><strong>service</strong></span></a>
73                </p>
74              </td>
75<td>
76                <p>
77                  Base class for all io_context services.
78                </p>
79              </td>
80</tr>
81<tr>
82<td>
83                <p>
84                  <a class="link" href="io_context__strand.html" title="io_context::strand"><span class="bold"><strong>strand</strong></span></a>
85                </p>
86              </td>
87<td>
88                <p>
89                  Provides serialised handler execution.
90                </p>
91              </td>
92</tr>
93<tr>
94<td>
95                <p>
96                  <a class="link" href="io_context__work.html" title="io_context::work"><span class="bold"><strong>work</strong></span></a>
97                </p>
98              </td>
99<td>
100                <p>
101                  (Deprecated: Use executor_work_guard.) Class to inform the io_context
102                  when it has work to do.
103                </p>
104              </td>
105</tr>
106<tr>
107<td>
108                <p>
109                  <a class="link" href="io_context/count_type.html" title="io_context::count_type"><span class="bold"><strong>count_type</strong></span></a>
110                </p>
111              </td>
112<td>
113                <p>
114                  The type used to count the number of handlers executed by the context.
115                </p>
116              </td>
117</tr>
118<tr>
119<td>
120                <p>
121                  <a class="link" href="io_context/executor_type.html" title="io_context::executor_type"><span class="bold"><strong>executor_type</strong></span></a>
122                </p>
123              </td>
124<td>
125                <p>
126                  Executor used to submit functions to an io_context.
127                </p>
128              </td>
129</tr>
130<tr>
131<td>
132                <p>
133                  <a class="link" href="io_context/fork_event.html" title="io_context::fork_event"><span class="bold"><strong>fork_event</strong></span></a>
134                </p>
135              </td>
136<td>
137                <p>
138                  Fork-related event notifications.
139                </p>
140              </td>
141</tr>
142</tbody>
143</table></div>
144<h5>
145<a name="boost_asio.reference.io_service.h1"></a>
146        <span class="phrase"><a name="boost_asio.reference.io_service.member_functions"></a></span><a class="link" href="io_service.html#boost_asio.reference.io_service.member_functions">Member
147        Functions</a>
148      </h5>
149<div class="informaltable"><table class="table">
150<colgroup>
151<col>
152<col>
153</colgroup>
154<thead><tr>
155<th>
156                <p>
157                  Name
158                </p>
159              </th>
160<th>
161                <p>
162                  Description
163                </p>
164              </th>
165</tr></thead>
166<tbody>
167<tr>
168<td>
169                <p>
170                  <a class="link" href="io_context/dispatch.html" title="io_context::dispatch"><span class="bold"><strong>dispatch</strong></span></a>
171                </p>
172              </td>
173<td>
174                <p>
175                  (Deprecated: Use boost::asio::dispatch().) Request the io_context
176                  to invoke the given handler.
177                </p>
178              </td>
179</tr>
180<tr>
181<td>
182                <p>
183                  <a class="link" href="io_context/get_executor.html" title="io_context::get_executor"><span class="bold"><strong>get_executor</strong></span></a>
184                </p>
185              </td>
186<td>
187                <p>
188                  Obtains the executor associated with the io_context.
189                </p>
190              </td>
191</tr>
192<tr>
193<td>
194                <p>
195                  <a class="link" href="io_context/io_context.html" title="io_context::io_context"><span class="bold"><strong>io_context</strong></span></a> <span class="silver">[constructor]</span>
196                </p>
197              </td>
198<td>
199                <p>
200                  Constructor.
201                </p>
202              </td>
203</tr>
204<tr>
205<td>
206                <p>
207                  <a class="link" href="io_context/notify_fork.html" title="io_context::notify_fork"><span class="bold"><strong>notify_fork</strong></span></a>
208                </p>
209              </td>
210<td>
211                <p>
212                  Notify the execution_context of a fork-related event.
213                </p>
214              </td>
215</tr>
216<tr>
217<td>
218                <p>
219                  <a class="link" href="io_context/poll.html" title="io_context::poll"><span class="bold"><strong>poll</strong></span></a>
220                </p>
221              </td>
222<td>
223                <p>
224                  Run the io_context object's event processing loop to execute ready
225                  handlers. <br> <span class="silver"> —</span><br> (Deprecated: Use non-error_code overload.)
226                  Run the io_context object's event processing loop to execute ready
227                  handlers.
228                </p>
229              </td>
230</tr>
231<tr>
232<td>
233                <p>
234                  <a class="link" href="io_context/poll_one.html" title="io_context::poll_one"><span class="bold"><strong>poll_one</strong></span></a>
235                </p>
236              </td>
237<td>
238                <p>
239                  Run the io_context object's event processing loop to execute one
240                  ready handler. <br> <span class="silver"> —</span><br> (Deprecated: Use non-error_code overload.)
241                  Run the io_context object's event processing loop to execute one
242                  ready handler.
243                </p>
244              </td>
245</tr>
246<tr>
247<td>
248                <p>
249                  <a class="link" href="io_context/post.html" title="io_context::post"><span class="bold"><strong>post</strong></span></a>
250                </p>
251              </td>
252<td>
253                <p>
254                  (Deprecated: Use boost::asio::post().) Request the io_context to
255                  invoke the given handler and return immediately.
256                </p>
257              </td>
258</tr>
259<tr>
260<td>
261                <p>
262                  <a class="link" href="io_context/reset.html" title="io_context::reset"><span class="bold"><strong>reset</strong></span></a>
263                </p>
264              </td>
265<td>
266                <p>
267                  (Deprecated: Use restart().) Reset the io_context in preparation
268                  for a subsequent run() invocation.
269                </p>
270              </td>
271</tr>
272<tr>
273<td>
274                <p>
275                  <a class="link" href="io_context/restart.html" title="io_context::restart"><span class="bold"><strong>restart</strong></span></a>
276                </p>
277              </td>
278<td>
279                <p>
280                  Restart the io_context in preparation for a subsequent run() invocation.
281                </p>
282              </td>
283</tr>
284<tr>
285<td>
286                <p>
287                  <a class="link" href="io_context/run.html" title="io_context::run"><span class="bold"><strong>run</strong></span></a>
288                </p>
289              </td>
290<td>
291                <p>
292                  Run the io_context object's event processing loop. <br> <span class="silver"> —</span><br>
293                  (Deprecated: Use non-error_code overload.) Run the io_context object's
294                  event processing loop.
295                </p>
296              </td>
297</tr>
298<tr>
299<td>
300                <p>
301                  <a class="link" href="io_context/run_for.html" title="io_context::run_for"><span class="bold"><strong>run_for</strong></span></a>
302                </p>
303              </td>
304<td>
305                <p>
306                  Run the io_context object's event processing loop for a specified
307                  duration.
308                </p>
309              </td>
310</tr>
311<tr>
312<td>
313                <p>
314                  <a class="link" href="io_context/run_one.html" title="io_context::run_one"><span class="bold"><strong>run_one</strong></span></a>
315                </p>
316              </td>
317<td>
318                <p>
319                  Run the io_context object's event processing loop to execute at
320                  most one handler. <br> <span class="silver"> —</span><br> (Deprecated: Use non-error_code
321                  overlaod.) Run the io_context object's event processing loop to
322                  execute at most one handler.
323                </p>
324              </td>
325</tr>
326<tr>
327<td>
328                <p>
329                  <a class="link" href="io_context/run_one_for.html" title="io_context::run_one_for"><span class="bold"><strong>run_one_for</strong></span></a>
330                </p>
331              </td>
332<td>
333                <p>
334                  Run the io_context object's event processing loop for a specified
335                  duration to execute at most one handler.
336                </p>
337              </td>
338</tr>
339<tr>
340<td>
341                <p>
342                  <a class="link" href="io_context/run_one_until.html" title="io_context::run_one_until"><span class="bold"><strong>run_one_until</strong></span></a>
343                </p>
344              </td>
345<td>
346                <p>
347                  Run the io_context object's event processing loop until a specified
348                  time to execute at most one handler.
349                </p>
350              </td>
351</tr>
352<tr>
353<td>
354                <p>
355                  <a class="link" href="io_context/run_until.html" title="io_context::run_until"><span class="bold"><strong>run_until</strong></span></a>
356                </p>
357              </td>
358<td>
359                <p>
360                  Run the io_context object's event processing loop until a specified
361                  time.
362                </p>
363              </td>
364</tr>
365<tr>
366<td>
367                <p>
368                  <a class="link" href="io_context/stop.html" title="io_context::stop"><span class="bold"><strong>stop</strong></span></a>
369                </p>
370              </td>
371<td>
372                <p>
373                  Stop the io_context object's event processing loop.
374                </p>
375              </td>
376</tr>
377<tr>
378<td>
379                <p>
380                  <a class="link" href="io_context/stopped.html" title="io_context::stopped"><span class="bold"><strong>stopped</strong></span></a>
381                </p>
382              </td>
383<td>
384                <p>
385                  Determine whether the io_context object has been stopped.
386                </p>
387              </td>
388</tr>
389<tr>
390<td>
391                <p>
392                  <a class="link" href="io_context/wrap.html" title="io_context::wrap"><span class="bold"><strong>wrap</strong></span></a>
393                </p>
394              </td>
395<td>
396                <p>
397                  (Deprecated: Use boost::asio::bind_executor().) Create a new handler
398                  that automatically dispatches the wrapped handler on the io_context.
399                </p>
400              </td>
401</tr>
402<tr>
403<td>
404                <p>
405                  <a class="link" href="io_context/_io_context.html" title="io_context::~io_context"><span class="bold"><strong>~io_context</strong></span></a> <span class="silver">[destructor]</span>
406                </p>
407              </td>
408<td>
409                <p>
410                  Destructor.
411                </p>
412              </td>
413</tr>
414</tbody>
415</table></div>
416<h5>
417<a name="boost_asio.reference.io_service.h2"></a>
418        <span class="phrase"><a name="boost_asio.reference.io_service.protected_member_functions"></a></span><a class="link" href="io_service.html#boost_asio.reference.io_service.protected_member_functions">Protected
419        Member Functions</a>
420      </h5>
421<div class="informaltable"><table class="table">
422<colgroup>
423<col>
424<col>
425</colgroup>
426<thead><tr>
427<th>
428                <p>
429                  Name
430                </p>
431              </th>
432<th>
433                <p>
434                  Description
435                </p>
436              </th>
437</tr></thead>
438<tbody>
439<tr>
440<td>
441                <p>
442                  <a class="link" href="io_context/destroy.html" title="io_context::destroy"><span class="bold"><strong>destroy</strong></span></a>
443                </p>
444              </td>
445<td>
446                <p>
447                  Destroys all services in the context.
448                </p>
449              </td>
450</tr>
451<tr>
452<td>
453                <p>
454                  <a class="link" href="io_context/shutdown.html" title="io_context::shutdown"><span class="bold"><strong>shutdown</strong></span></a>
455                </p>
456              </td>
457<td>
458                <p>
459                  Shuts down all services in the context.
460                </p>
461              </td>
462</tr>
463</tbody>
464</table></div>
465<h5>
466<a name="boost_asio.reference.io_service.h3"></a>
467        <span class="phrase"><a name="boost_asio.reference.io_service.friends"></a></span><a class="link" href="io_service.html#boost_asio.reference.io_service.friends">Friends</a>
468      </h5>
469<div class="informaltable"><table class="table">
470<colgroup>
471<col>
472<col>
473</colgroup>
474<thead><tr>
475<th>
476                <p>
477                  Name
478                </p>
479              </th>
480<th>
481                <p>
482                  Description
483                </p>
484              </th>
485</tr></thead>
486<tbody>
487<tr>
488<td>
489                <p>
490                  <a class="link" href="io_context/add_service.html" title="io_context::add_service"><span class="bold"><strong>add_service</strong></span></a>
491                </p>
492              </td>
493<td>
494                <p>
495                  (Deprecated: Use make_service().) Add a service object to the execution_context.
496                </p>
497              </td>
498</tr>
499<tr>
500<td>
501                <p>
502                  <a class="link" href="io_context/has_service.html" title="io_context::has_service"><span class="bold"><strong>has_service</strong></span></a>
503                </p>
504              </td>
505<td>
506                <p>
507                  Determine if an execution_context contains a specified service
508                  type.
509                </p>
510              </td>
511</tr>
512<tr>
513<td>
514                <p>
515                  <a class="link" href="io_context/make_service.html" title="io_context::make_service"><span class="bold"><strong>make_service</strong></span></a>
516                </p>
517              </td>
518<td>
519                <p>
520                  Creates a service object and adds it to the execution_context.
521                </p>
522              </td>
523</tr>
524<tr>
525<td>
526                <p>
527                  <a class="link" href="io_context/use_service.html" title="io_context::use_service"><span class="bold"><strong>use_service</strong></span></a>
528                </p>
529              </td>
530<td>
531                <p>
532                  <br> <span class="silver"> —</span><br> Obtain the service object corresponding to the given
533                  type.
534                </p>
535              </td>
536</tr>
537</tbody>
538</table></div>
539<p>
540        The <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
541        class provides the core I/O functionality for users of the asynchronous I/O
542        objects, including:
543      </p>
544<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
545<li class="listitem">
546            <code class="computeroutput">boost::asio::ip::tcp::socket</code>
547          </li>
548<li class="listitem">
549            <code class="computeroutput">boost::asio::ip::tcp::acceptor</code>
550          </li>
551<li class="listitem">
552            <code class="computeroutput">boost::asio::ip::udp::socket</code>
553          </li>
554<li class="listitem">
555            <a class="link" href="deadline_timer.html" title="deadline_timer"><code class="computeroutput">deadline_timer</code></a>.
556          </li>
557</ul></div>
558<p>
559        The <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
560        class also includes facilities intended for developers of custom asynchronous
561        services.
562      </p>
563<h5>
564<a name="boost_asio.reference.io_service.h4"></a>
565        <span class="phrase"><a name="boost_asio.reference.io_service.thread_safety"></a></span><a class="link" href="io_service.html#boost_asio.reference.io_service.thread_safety">Thread
566        Safety</a>
567      </h5>
568<p>
569        <span class="emphasis"><em>Distinct</em></span> <span class="emphasis"><em>objects:</em></span> Safe.
570      </p>
571<p>
572        <span class="emphasis"><em>Shared</em></span> <span class="emphasis"><em>objects:</em></span> Safe, with the
573        specific exceptions of the <code class="computeroutput">restart()</code> and <code class="computeroutput">notify_fork()</code>
574        functions. Calling <code class="computeroutput">restart()</code> while there are unfinished <code class="computeroutput">run()</code>,
575        <code class="computeroutput">run_one()</code>, <code class="computeroutput">run_for()</code>, <code class="computeroutput">run_until()</code>,
576        <code class="computeroutput">poll()</code> or <code class="computeroutput">poll_one()</code> calls results in undefined
577        behaviour. The <code class="computeroutput">notify_fork()</code> function should not be called while
578        any <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
579        function, or any function on an I/O object that is associated with the <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>,
580        is being called in another thread.
581      </p>
582<h5>
583<a name="boost_asio.reference.io_service.h5"></a>
584        <span class="phrase"><a name="boost_asio.reference.io_service.synchronous_and_asynchronous_operations"></a></span><a class="link" href="io_service.html#boost_asio.reference.io_service.synchronous_and_asynchronous_operations">Synchronous
585        and asynchronous operations</a>
586      </h5>
587<p>
588        Synchronous operations on I/O objects implicitly run the <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
589        object for an individual operation. The <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
590        functions <code class="computeroutput">run()</code>, <code class="computeroutput">run_one()</code>, <code class="computeroutput">run_for()</code>,
591        <code class="computeroutput">run_until()</code>, <code class="computeroutput">poll()</code> or <code class="computeroutput">poll_one()</code>
592        must be called for the <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
593        to perform asynchronous operations on behalf of a C++ program. Notification
594        that an asynchronous operation has completed is delivered by invocation of
595        the associated handler. Handlers are invoked only by a thread that is currently
596        calling any overload of <code class="computeroutput">run()</code>, <code class="computeroutput">run_one()</code>, <code class="computeroutput">run_for()</code>,
597        <code class="computeroutput">run_until()</code>, <code class="computeroutput">poll()</code> or <code class="computeroutput">poll_one()</code>
598        for the <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>.
599      </p>
600<h5>
601<a name="boost_asio.reference.io_service.h6"></a>
602        <span class="phrase"><a name="boost_asio.reference.io_service.effect_of_exceptions_thrown_from_handlers"></a></span><a class="link" href="io_service.html#boost_asio.reference.io_service.effect_of_exceptions_thrown_from_handlers">Effect
603        of exceptions thrown from handlers</a>
604      </h5>
605<p>
606        If an exception is thrown from a handler, the exception is allowed to propagate
607        through the throwing thread's invocation of <code class="computeroutput">run()</code>, <code class="computeroutput">run_one()</code>,
608        <code class="computeroutput">run_for()</code>, <code class="computeroutput">run_until()</code>, <code class="computeroutput">poll()</code> or
609        <code class="computeroutput">poll_one()</code>. No other threads that are calling any of these functions
610        are affected. It is then the responsibility of the application to catch the
611        exception.
612      </p>
613<p>
614        After the exception has been caught, the <code class="computeroutput">run()</code>, <code class="computeroutput">run_one()</code>,
615        <code class="computeroutput">run_for()</code>, <code class="computeroutput">run_until()</code>, <code class="computeroutput">poll()</code> or
616        <code class="computeroutput">poll_one()</code> call may be restarted <span class="emphasis"><em>without</em></span>
617        the need for an intervening call to <code class="computeroutput">restart()</code>. This allows the
618        thread to rejoin the <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
619        object's thread pool without impacting any other threads in the pool.
620      </p>
621<p>
622        For example:
623      </p>
624<pre class="programlisting">boost::asio::io_context io_context;
625...
626for (;;)
627{
628  try
629  {
630    io_context.run();
631    break; // run() exited normally
632  }
633  catch (my_exception&amp; e)
634  {
635    // Deal with exception as appropriate.
636  }
637}
638</pre>
639<h5>
640<a name="boost_asio.reference.io_service.h7"></a>
641        <span class="phrase"><a name="boost_asio.reference.io_service.submitting_arbitrary_tasks_to_the_io_context"></a></span><a class="link" href="io_service.html#boost_asio.reference.io_service.submitting_arbitrary_tasks_to_the_io_context">Submitting
642        arbitrary tasks to the io_context</a>
643      </h5>
644<p>
645        To submit functions to the <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>,
646        use the <a class="link" href="dispatch.html" title="dispatch"><code class="computeroutput">dispatch</code></a>
647        , <a class="link" href="post.html" title="post"><code class="computeroutput">post</code></a> or
648        <a class="link" href="defer.html" title="defer"><code class="computeroutput">defer</code></a> free
649        functions.
650      </p>
651<p>
652        For example:
653      </p>
654<pre class="programlisting">void my_task()
655{
656  ...
657}
658
659...
660
661boost::asio::io_context io_context;
662
663// Submit a function to the io_context.
664boost::asio::post(io_context, my_task);
665
666// Submit a lambda object to the io_context.
667boost::asio::post(io_context,
668    []()
669    {
670      ...
671    });
672
673// Run the io_context until it runs out of work.
674io_context.run();
675</pre>
676<h5>
677<a name="boost_asio.reference.io_service.h8"></a>
678        <span class="phrase"><a name="boost_asio.reference.io_service.stopping_the_io_context_from_running_out_of_work"></a></span><a class="link" href="io_service.html#boost_asio.reference.io_service.stopping_the_io_context_from_running_out_of_work">Stopping
679        the io_context from running out of work</a>
680      </h5>
681<p>
682        Some applications may need to prevent an <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
683        object's <code class="computeroutput">run()</code> call from returning when there is no more work
684        to do. For example, the <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
685        may be being run in a background thread that is launched prior to the application's
686        asynchronous operations. The <code class="computeroutput">run()</code> call may be kept running
687        by creating an executor that tracks work against the <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>:
688      </p>
689<pre class="programlisting">boost::asio::io_context io_context;
690auto work = boost::asio::require(io_context.get_executor(),
691    boost::asio::execution::outstanding_work.tracked);
692...
693</pre>
694<p>
695        If using C++03, which lacks automatic variable type deduction, you may compute
696        the return type of the require call:
697      </p>
698<pre class="programlisting">boost::asio::io_context io_context;
699typename boost::asio::require_result&lt;
700    boost::asio::io_context::executor_type,
701    boost::asio::exeution::outstanding_work_t::tracked_t&gt;
702  work = boost::asio::require(io_context.get_executor(),
703    boost::asio::execution::outstanding_work.tracked);
704...
705</pre>
706<p>
707        or store the result in the type-erasing executor wrapper, any_io_executor:
708      </p>
709<pre class="programlisting">boost::asio::io_context io_context;
710boost::asio::any_io_executor work
711  = boost::asio::require(io_context.get_executor(),
712      boost::asio::execution::outstanding_work.tracked);
713...
714</pre>
715<p>
716        To effect a shutdown, the application will then need to call the <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
717        object's <code class="computeroutput">stop()</code> member function. This will cause the <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
718        <code class="computeroutput">run()</code> call to return as soon as possible, abandoning unfinished
719        operations and without permitting ready handlers to be dispatched.
720      </p>
721<p>
722        Alternatively, if the application requires that all operations and handlers
723        be allowed to finish normally, store the work-tracking executor in an any_io_executor
724        object, so that it may be explicitly reset.
725      </p>
726<pre class="programlisting">boost::asio::io_context io_context;
727boost::asio::any_io_executor work
728  = boost::asio::require(io_context.get_executor(),
729      boost::asio::execution::outstanding_work.tracked);
730...
731work = boost::asio::any_io_executor(); // Allow run() to exit.
732</pre>
733<h5>
734<a name="boost_asio.reference.io_service.h9"></a>
735        <span class="phrase"><a name="boost_asio.reference.io_service.requirements"></a></span><a class="link" href="io_service.html#boost_asio.reference.io_service.requirements">Requirements</a>
736      </h5>
737<p>
738        <span class="emphasis"><em>Header: </em></span><code class="literal">boost/asio/io_service.hpp</code>
739      </p>
740<p>
741        <span class="emphasis"><em>Convenience header: </em></span><code class="literal">boost/asio.hpp</code>
742      </p>
743</div>
744<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
745<td align="left"></td>
746<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
747      Kohlhoff<p>
748        Distributed under the Boost Software License, Version 1.0. (See accompanying
749        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>)
750      </p>
751</div></td>
752</tr></table>
753<hr>
754<div class="spirit-nav">
755<a accesskey="p" href="io_context__work/_work.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.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="ip__address.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
756</div>
757</body>
758</html>
759