3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Threads and Boost.Asio</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="async.html" title="The Proactor Design Pattern: Concurrency Without Threads">
10 <link rel="next" href="strands.html" title="Strands: Use Threads Without Explicit Locking">
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>
22 <div class="spirit-nav">
23 <a accesskey="p" href="async.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="strands.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h4 class="title">
27 <a name="boost_asio.overview.core.threads"></a><a class="link" href="threads.html" title="Threads and Boost.Asio">Threads and Boost.Asio</a>
28 </h4></div></div></div>
30 <a name="boost_asio.overview.core.threads.h0"></a>
31 <span class="phrase"><a name="boost_asio.overview.core.threads.thread_safety"></a></span><a class="link" href="threads.html#boost_asio.overview.core.threads.thread_safety">Thread
35 In general, it is safe to make concurrent use of distinct objects, but
36 unsafe to make concurrent use of a single object. However, types such as
37 <code class="computeroutput"><span class="identifier">io_service</span></code> provide a stronger
38 guarantee that it is safe to use a single object concurrently.
41 <a name="boost_asio.overview.core.threads.h1"></a>
42 <span class="phrase"><a name="boost_asio.overview.core.threads.thread_pools"></a></span><a class="link" href="threads.html#boost_asio.overview.core.threads.thread_pools">Thread
46 Multiple threads may call <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">run</span><span class="special">()</span></code> to set up a pool of threads from which
47 completion handlers may be invoked. This approach may also be used with
48 <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">post</span><span class="special">()</span></code>
49 to use a means to perform any computational tasks across a thread pool.
52 Note that all threads that have joined an <code class="computeroutput"><span class="identifier">io_service</span></code>'s
53 pool are considered equivalent, and the <code class="computeroutput"><span class="identifier">io_service</span></code>
54 may distribute work across them in an arbitrary fashion.
57 <a name="boost_asio.overview.core.threads.h2"></a>
58 <span class="phrase"><a name="boost_asio.overview.core.threads.internal_threads"></a></span><a class="link" href="threads.html#boost_asio.overview.core.threads.internal_threads">Internal
62 The implementation of this library for a particular platform may make use
63 of one or more internal threads to emulate asynchronicity. As far as possible,
64 these threads must be invisible to the library user. In particular, the
67 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
69 must not call the user's code directly; and
72 must block all signals.
76 This approach is complemented by the following guarantee:
78 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
79 Asynchronous completion handlers will only be called from threads that
80 are currently calling <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">run</span><span class="special">()</span></code>.
83 Consequently, it is the library user's responsibility to create and manage
84 all threads to which the notifications will be delivered.
87 The reasons for this approach include:
89 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
91 By only calling <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">run</span><span class="special">()</span></code> from a single thread, the user's
92 code can avoid the development complexity associated with synchronisation.
93 For example, a library user can implement scalable servers that are
94 single-threaded (from the user's point of view).
97 A library user may need to perform initialisation in a thread shortly
98 after the thread starts and before any other application code is executed.
99 For example, users of Microsoft's COM must call <code class="computeroutput"><span class="identifier">CoInitializeEx</span></code>
100 before any other COM operations can be called from that thread.
102 <li class="listitem">
103 The library interface is decoupled from interfaces for thread creation
104 and management, and permits implementations on platforms where threads
109 <a name="boost_asio.overview.core.threads.h3"></a>
110 <span class="phrase"><a name="boost_asio.overview.core.threads.see_also"></a></span><a class="link" href="threads.html#boost_asio.overview.core.threads.see_also">See
114 <a class="link" href="../../reference/io_service.html" title="io_service">io_service</a>.
117 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
118 <td align="left"></td>
119 <td align="right"><div class="copyright-footer">Copyright © 2003-2017 Christopher M. Kohlhoff<p>
120 Distributed under the Boost Software License, Version 1.0. (See accompanying
121 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>)
126 <div class="spirit-nav">
127 <a accesskey="p" href="async.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="strands.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>