Imported Upstream version 1.64.0
[platform/upstream/boost.git] / doc / html / boost_asio / overview / core / threads.html
1 <html>
2 <head>
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">
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="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>
24 </div>
25 <div class="section">
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>
29 <h6>
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
32           Safety</a>
33         </h6>
34 <p>
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.
39         </p>
40 <h6>
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
43           Pools</a>
44         </h6>
45 <p>
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.
50         </p>
51 <p>
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.
55         </p>
56 <h6>
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
59           Threads</a>
60         </h6>
61 <p>
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
65           threads:
66         </p>
67 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
68 <li class="listitem">
69               must not call the user's code directly; and
70             </li>
71 <li class="listitem">
72               must block all signals.
73             </li>
74 </ul></div>
75 <p>
76           This approach is complemented by the following guarantee:
77         </p>
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>.
81             </li></ul></div>
82 <p>
83           Consequently, it is the library user's responsibility to create and manage
84           all threads to which the notifications will be delivered.
85         </p>
86 <p>
87           The reasons for this approach include:
88         </p>
89 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
90 <li class="listitem">
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).
95             </li>
96 <li class="listitem">
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.
101             </li>
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
105               are not available.
106             </li>
107 </ul></div>
108 <h6>
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
111           Also</a>
112         </h6>
113 <p>
114           <a class="link" href="../../reference/io_service.html" title="io_service">io_service</a>.
115         </p>
116 </div>
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 &#169; 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>)
122       </p>
123 </div></td>
124 </tr></table>
125 <hr>
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>
128 </div>
129 </body>
130 </html>