• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>thread_pool</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="this_coro__executor_t/executor_t.html" title="this_coro::executor_t::executor_t">
10<link rel="next" href="thread_pool/add_service.html" title="thread_pool::add_service">
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="this_coro__executor_t/executor_t.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="thread_pool/add_service.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.thread_pool"></a><a class="link" href="thread_pool.html" title="thread_pool">thread_pool</a>
28</h3></div></div></div>
29<p>
30        A simple fixed-size thread pool.
31      </p>
32<pre class="programlisting">class thread_pool :
33  public execution_context
34</pre>
35<h5>
36<a name="boost_asio.reference.thread_pool.h0"></a>
37        <span class="phrase"><a name="boost_asio.reference.thread_pool.types"></a></span><a class="link" href="thread_pool.html#boost_asio.reference.thread_pool.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="thread_pool__basic_executor_type.html" title="thread_pool::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 a thread
66                  pool.
67                </p>
68              </td>
69</tr>
70<tr>
71<td>
72                <p>
73                  <a class="link" href="thread_pool/executor_type.html" title="thread_pool::executor_type"><span class="bold"><strong>executor_type</strong></span></a>
74                </p>
75              </td>
76<td>
77                <p>
78                  Executor used to submit functions to a thread pool.
79                </p>
80              </td>
81</tr>
82<tr>
83<td>
84                <p>
85                  <a class="link" href="thread_pool/fork_event.html" title="thread_pool::fork_event"><span class="bold"><strong>fork_event</strong></span></a>
86                </p>
87              </td>
88<td>
89                <p>
90                  Fork-related event notifications.
91                </p>
92              </td>
93</tr>
94<tr>
95<td>
96                <p>
97                  <a class="link" href="thread_pool/scheduler_type.html" title="thread_pool::scheduler_type"><span class="bold"><strong>scheduler_type</strong></span></a>
98                </p>
99              </td>
100<td>
101                <p>
102                  Scheduler used to schedule receivers on a thread pool.
103                </p>
104              </td>
105</tr>
106</tbody>
107</table></div>
108<h5>
109<a name="boost_asio.reference.thread_pool.h1"></a>
110        <span class="phrase"><a name="boost_asio.reference.thread_pool.member_functions"></a></span><a class="link" href="thread_pool.html#boost_asio.reference.thread_pool.member_functions">Member
111        Functions</a>
112      </h5>
113<div class="informaltable"><table class="table">
114<colgroup>
115<col>
116<col>
117</colgroup>
118<thead><tr>
119<th>
120                <p>
121                  Name
122                </p>
123              </th>
124<th>
125                <p>
126                  Description
127                </p>
128              </th>
129</tr></thead>
130<tbody>
131<tr>
132<td>
133                <p>
134                  <a class="link" href="thread_pool/attach.html" title="thread_pool::attach"><span class="bold"><strong>attach</strong></span></a>
135                </p>
136              </td>
137<td>
138                <p>
139                  Attaches the current thread to the pool.
140                </p>
141              </td>
142</tr>
143<tr>
144<td>
145                <p>
146                  <a class="link" href="thread_pool/executor.html" title="thread_pool::executor"><span class="bold"><strong>executor</strong></span></a>
147                </p>
148              </td>
149<td>
150                <p>
151                  Obtains the executor associated with the pool.
152                </p>
153              </td>
154</tr>
155<tr>
156<td>
157                <p>
158                  <a class="link" href="thread_pool/get_executor.html" title="thread_pool::get_executor"><span class="bold"><strong>get_executor</strong></span></a>
159                </p>
160              </td>
161<td>
162                <p>
163                  Obtains the executor associated with the pool.
164                </p>
165              </td>
166</tr>
167<tr>
168<td>
169                <p>
170                  <a class="link" href="thread_pool/join.html" title="thread_pool::join"><span class="bold"><strong>join</strong></span></a>
171                </p>
172              </td>
173<td>
174                <p>
175                  Joins the threads.
176                </p>
177              </td>
178</tr>
179<tr>
180<td>
181                <p>
182                  <a class="link" href="thread_pool/notify_fork.html" title="thread_pool::notify_fork"><span class="bold"><strong>notify_fork</strong></span></a>
183                </p>
184              </td>
185<td>
186                <p>
187                  Notify the execution_context of a fork-related event.
188                </p>
189              </td>
190</tr>
191<tr>
192<td>
193                <p>
194                  <a class="link" href="thread_pool/scheduler.html" title="thread_pool::scheduler"><span class="bold"><strong>scheduler</strong></span></a>
195                </p>
196              </td>
197<td>
198                <p>
199                  Obtains the scheduler associated with the pool.
200                </p>
201              </td>
202</tr>
203<tr>
204<td>
205                <p>
206                  <a class="link" href="thread_pool/stop.html" title="thread_pool::stop"><span class="bold"><strong>stop</strong></span></a>
207                </p>
208              </td>
209<td>
210                <p>
211                  Stops the threads.
212                </p>
213              </td>
214</tr>
215<tr>
216<td>
217                <p>
218                  <a class="link" href="thread_pool/thread_pool.html" title="thread_pool::thread_pool"><span class="bold"><strong>thread_pool</strong></span></a> <span class="silver">[constructor]</span>
219                </p>
220              </td>
221<td>
222                <p>
223                  Constructs a pool with an automatically determined number of threads.
224                  <br> <span class="silver"> —</span><br> Constructs a pool with a specified number of threads.
225                </p>
226              </td>
227</tr>
228<tr>
229<td>
230                <p>
231                  <a class="link" href="thread_pool/wait.html" title="thread_pool::wait"><span class="bold"><strong>wait</strong></span></a>
232                </p>
233              </td>
234<td>
235                <p>
236                  Waits for threads to complete.
237                </p>
238              </td>
239</tr>
240<tr>
241<td>
242                <p>
243                  <a class="link" href="thread_pool/_thread_pool.html" title="thread_pool::~thread_pool"><span class="bold"><strong>~thread_pool</strong></span></a> <span class="silver">[destructor]</span>
244                </p>
245              </td>
246<td>
247                <p>
248                  Destructor.
249                </p>
250              </td>
251</tr>
252</tbody>
253</table></div>
254<h5>
255<a name="boost_asio.reference.thread_pool.h2"></a>
256        <span class="phrase"><a name="boost_asio.reference.thread_pool.protected_member_functions"></a></span><a class="link" href="thread_pool.html#boost_asio.reference.thread_pool.protected_member_functions">Protected
257        Member Functions</a>
258      </h5>
259<div class="informaltable"><table class="table">
260<colgroup>
261<col>
262<col>
263</colgroup>
264<thead><tr>
265<th>
266                <p>
267                  Name
268                </p>
269              </th>
270<th>
271                <p>
272                  Description
273                </p>
274              </th>
275</tr></thead>
276<tbody>
277<tr>
278<td>
279                <p>
280                  <a class="link" href="thread_pool/destroy.html" title="thread_pool::destroy"><span class="bold"><strong>destroy</strong></span></a>
281                </p>
282              </td>
283<td>
284                <p>
285                  Destroys all services in the context.
286                </p>
287              </td>
288</tr>
289<tr>
290<td>
291                <p>
292                  <a class="link" href="thread_pool/shutdown.html" title="thread_pool::shutdown"><span class="bold"><strong>shutdown</strong></span></a>
293                </p>
294              </td>
295<td>
296                <p>
297                  Shuts down all services in the context.
298                </p>
299              </td>
300</tr>
301</tbody>
302</table></div>
303<h5>
304<a name="boost_asio.reference.thread_pool.h3"></a>
305        <span class="phrase"><a name="boost_asio.reference.thread_pool.friends"></a></span><a class="link" href="thread_pool.html#boost_asio.reference.thread_pool.friends">Friends</a>
306      </h5>
307<div class="informaltable"><table class="table">
308<colgroup>
309<col>
310<col>
311</colgroup>
312<thead><tr>
313<th>
314                <p>
315                  Name
316                </p>
317              </th>
318<th>
319                <p>
320                  Description
321                </p>
322              </th>
323</tr></thead>
324<tbody>
325<tr>
326<td>
327                <p>
328                  <a class="link" href="thread_pool/add_service.html" title="thread_pool::add_service"><span class="bold"><strong>add_service</strong></span></a>
329                </p>
330              </td>
331<td>
332                <p>
333                  (Deprecated: Use make_service().) Add a service object to the execution_context.
334                </p>
335              </td>
336</tr>
337<tr>
338<td>
339                <p>
340                  <a class="link" href="thread_pool/has_service.html" title="thread_pool::has_service"><span class="bold"><strong>has_service</strong></span></a>
341                </p>
342              </td>
343<td>
344                <p>
345                  Determine if an execution_context contains a specified service
346                  type.
347                </p>
348              </td>
349</tr>
350<tr>
351<td>
352                <p>
353                  <a class="link" href="thread_pool/make_service.html" title="thread_pool::make_service"><span class="bold"><strong>make_service</strong></span></a>
354                </p>
355              </td>
356<td>
357                <p>
358                  Creates a service object and adds it to the execution_context.
359                </p>
360              </td>
361</tr>
362<tr>
363<td>
364                <p>
365                  <a class="link" href="thread_pool/use_service.html" title="thread_pool::use_service"><span class="bold"><strong>use_service</strong></span></a>
366                </p>
367              </td>
368<td>
369                <p>
370                  Obtain the service object corresponding to the given type.
371                </p>
372              </td>
373</tr>
374</tbody>
375</table></div>
376<p>
377        The thread pool class is an execution context where functions are permitted
378        to run on one of a fixed number of threads.
379      </p>
380<h5>
381<a name="boost_asio.reference.thread_pool.h4"></a>
382        <span class="phrase"><a name="boost_asio.reference.thread_pool.submitting_tasks_to_the_pool"></a></span><a class="link" href="thread_pool.html#boost_asio.reference.thread_pool.submitting_tasks_to_the_pool">Submitting
383        tasks to the pool</a>
384      </h5>
385<p>
386        To submit functions to the thread pool, use the <a class="link" href="dispatch.html" title="dispatch"><code class="computeroutput">dispatch</code></a>
387        , <a class="link" href="post.html" title="post"><code class="computeroutput">post</code></a> or
388        <a class="link" href="defer.html" title="defer"><code class="computeroutput">defer</code></a> free
389        functions.
390      </p>
391<p>
392        For example:
393      </p>
394<pre class="programlisting">void my_task()
395{
396  ...
397}
398
399...
400
401// Launch the pool with four threads.
402boost::asio::thread_pool pool(4);
403
404// Submit a function to the pool.
405boost::asio::post(pool, my_task);
406
407// Submit a lambda object to the pool.
408boost::asio::post(pool,
409    []()
410    {
411      ...
412    });
413
414// Wait for all tasks in the pool to complete.
415pool.join();
416</pre>
417<h5>
418<a name="boost_asio.reference.thread_pool.h5"></a>
419        <span class="phrase"><a name="boost_asio.reference.thread_pool.requirements"></a></span><a class="link" href="thread_pool.html#boost_asio.reference.thread_pool.requirements">Requirements</a>
420      </h5>
421<p>
422        <span class="emphasis"><em>Header: </em></span><code class="literal">boost/asio/thread_pool.hpp</code>
423      </p>
424<p>
425        <span class="emphasis"><em>Convenience header: </em></span><code class="literal">boost/asio.hpp</code>
426      </p>
427</div>
428<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
429<td align="left"></td>
430<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
431      Kohlhoff<p>
432        Distributed under the Boost Software License, Version 1.0. (See accompanying
433        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>)
434      </p>
435</div></td>
436</tr></table>
437<hr>
438<div class="spirit-nav">
439<a accesskey="p" href="this_coro__executor_t/executor_t.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="thread_pool/add_service.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
440</div>
441</body>
442</html>
443