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

...

asio::io_context io_context;

// Submit a function to the io_context.
asio::post(io_context, my_task);

// Submit a lambda object to the io_context.
asio::post(io_context,
    []()
    {
      ...
    });

// Run the io_context until it runs out of work.
io_context.run();
</pre>
<h5>
<a name="asio.reference.io_service.h8"></a>
        <span><a name="asio.reference.io_service.stopping_the_io_context_from_running_out_of_work"></a></span><a class="link" href="io_service.html#asio.reference.io_service.stopping_the_io_context_from_running_out_of_work">Stopping
        the io_context from running out of work</a>
      </h5>
<p>
        Some applications may need to prevent an <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
        object's <code class="computeroutput">run()</code> call from returning when there is no more work
        to do. For example, the <a class="link" href="io_context.html" title="io_context"><code class="computeroutput">io_context</code></a>
        may be being run in a background thread that is launched prior to the application's
        asynchronous operations. The <code class="computeroutput">run()</code> call may be kept running
        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>:
      </p>
<pre class="programlisting">asio::io_context io_context;
auto work = asio::require(io_context.get_executor(),
    asio::execution::outstanding_work.tracked);
...
</pre>
<p>
        If using C++03, which lacks automatic variable type deduction, you may compute
        the return type of the require call:
      </p>
<pre class="programlisting">asio::io_context io_context;
typename asio::require_result&lt;
    asio::io_context::executor_type,
    asio::exeution::outstanding_work_t::tracked_t&gt;
  work = asio::require(io_context.get_executor(),
    asio::execution::outstanding_work.tracked);
...
</pre>
<p>
        or store the result in the type-erasing executor wrapper, any_io_executor:
      </p>
<pre class="programlisting">asio::io_context io_context;
asio::any_io_executor work
  = asio::require(io_context.get_executor(),
      asio::execution::outstanding_work.tracked);
...
</pre>
<p>
        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>
        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>
        <code class="computeroutput">run()</code> call to return as soon as possible, abandoning unfinished
        operations and without permitting ready handlers to be dispatched.
      </p>
<p>
        Alternatively, if the application requires that all operations and handlers
        be allowed to finish normally, store the work-tracking executor in an any_io_executor
        object, so that it may be explicitly reset.
      </p>
<pre class="programlisting">asio::io_context io_context;
asio::any_io_executor work
  = asio::require(io_context.get_executor(),
      asio::execution::outstanding_work.tracked);
...
work = asio::any_io_executor(); // Allow run() to exit.
</pre>
<h5>
<a name="asio.reference.io_service.h9"></a>
        <span><a name="asio.reference.io_service.requirements"></a></span><a class="link" href="io_service.html#asio.reference.io_service.requirements">Requirements</a>
      </h5>
<p>
        <span class="emphasis"><em>Header: </em></span><code class="literal">asio/io_service.hpp</code>
      </p>
<p>
        <span class="emphasis"><em>Convenience header: </em></span><code class="literal">asio.hpp</code>
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
      Kohlhoff<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        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>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="io_context__work/_work.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="ip__address.html"><img src="../../next.png" alt="Next"></a>
</div>
</body>
</html>