• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Running with worker threads</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="../index.html" title="Chapter 1. Fiber">
8<link rel="up" href="../index.html" title="Chapter 1. Fiber">
9<link rel="prev" href="gpu_computing/hip.html" title="ROCm/HIP">
10<link rel="next" href="performance.html" title="Performance">
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="gpu_computing/hip.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="performance.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h2 class="title" style="clear: both">
27<a name="fiber.worker"></a><a name="worker"></a><a class="link" href="worker.html" title="Running with worker threads">Running with worker
28    threads</a>
29</h2></div></div></div>
30<h4>
31<a name="fiber.worker.h0"></a>
32      <span class="phrase"><a name="fiber.worker.keep_workers_running"></a></span><a class="link" href="worker.html#fiber.worker.keep_workers_running">Keep
33      workers running</a>
34    </h4>
35<p>
36      If a worker thread is used but no fiber is created or parts of the framework
37      (like <a class="link" href="fiber_mgmt/this_fiber.html#this_fiber_yield"><code class="computeroutput">this_fiber::yield()</code></a>) are touched, then no fiber scheduler
38      is instantiated.
39    </p>
40<pre class="programlisting"><span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span>
41        <span class="special">[]{</span>
42            <span class="comment">// fiber scheduler not instantiated</span>
43        <span class="special">});</span>
44<span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span>
45</pre>
46<p>
47      If <span class="emphasis"><em>use_scheduling_algorithm&lt;&gt;()</em></span> is invoked, the
48      fiber scheduler is created. If the worker thread simply returns, destroys the
49      scheduler and terminates.
50    </p>
51<pre class="programlisting"><span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span>
52        <span class="special">[]{</span>
53            <span class="comment">// fiber scheduler created</span>
54            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span><span class="identifier">my_fiber_scheduler</span><span class="special">&gt;();</span>
55        <span class="special">});</span>
56<span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span>
57</pre>
58<p>
59      In order to keep the worker thread running, the fiber associated with the thread
60      stack (which is called <span class="quote">“<span class="quote">main</span>”</span> fiber) is blocked. For instance
61      the <span class="quote">“<span class="quote">main</span>”</span> fiber might wait on a <a class="link" href="synchronization/conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a>.
62      For a gracefully shutdown <a class="link" href="synchronization/conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> is signalled
63      and the <span class="quote">“<span class="quote">main</span>”</span> fiber returns. The scheduler gets destructed if
64      all fibers of the worker thread have been terminated.
65    </p>
66<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="identifier">mtx</span><span class="special">;</span>
67<span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">condition_variable_any</span> <span class="identifier">cv</span><span class="special">;</span>
68<span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span>
69        <span class="special">[&amp;</span><span class="identifier">mtx</span><span class="special">,&amp;</span><span class="identifier">cv</span><span class="special">]{</span>
70            <span class="identifier">mtx</span><span class="special">.</span><span class="identifier">lock</span><span class="special">();</span>
71            <span class="comment">// suspend till signalled</span>
72            <span class="identifier">cv</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span><span class="identifier">mtx</span><span class="special">);</span>
73            <span class="identifier">mtx</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">();</span>
74        <span class="special">});</span>
75<span class="comment">// signal termination</span>
76<span class="identifier">cv</span><span class="special">.</span><span class="identifier">notify_all</span><span class="special">();</span>
77<span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span>
78</pre>
79<h4>
80<a name="fiber.worker.h1"></a>
81      <span class="phrase"><a name="fiber.worker.processing_tasks"></a></span><a class="link" href="worker.html#fiber.worker.processing_tasks">Processing
82      tasks</a>
83    </h4>
84<p>
85      Tasks can be transferred via channels. The worker thread runs a pool of fibers
86      that dequeue and executed tasks from the channel. The termination is signalled
87      via closing the channel.
88    </p>
89<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">task</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">function</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">()&gt;;</span>
90<span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span><span class="identifier">task</span><span class="special">&gt;</span> <span class="identifier">ch</span><span class="special">{</span><span class="number">1024</span><span class="special">};</span>
91<span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span>
92        <span class="special">[&amp;</span><span class="identifier">ch</span><span class="special">]{</span>
93            <span class="comment">// create pool of fibers</span>
94            <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">=</span><span class="number">0</span><span class="special">;</span> <span class="identifier">i</span><span class="special">&lt;</span><span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
95                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">{</span>
96                    <span class="special">[&amp;</span><span class="identifier">ch</span><span class="special">]{</span>
97                        <span class="identifier">task</span> <span class="identifier">tsk</span><span class="special">;</span>
98                        <span class="comment">// dequeue and process tasks</span>
99                        <span class="keyword">while</span> <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">closed</span><span class="special">!=</span><span class="identifier">ch</span><span class="special">.</span><span class="identifier">pop</span><span class="special">(</span><span class="identifier">tsk</span><span class="special">)){</span>
100                            <span class="identifier">tsk</span><span class="special">();</span>
101                        <span class="special">}</span>
102                    <span class="special">}}.</span><span class="identifier">detach</span><span class="special">();</span>
103            <span class="special">}</span>
104            <span class="identifier">task</span> <span class="identifier">tsk</span><span class="special">;</span>
105            <span class="comment">// dequeue and process tasks</span>
106            <span class="keyword">while</span> <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">closed</span><span class="special">!=</span><span class="identifier">ch</span><span class="special">.</span><span class="identifier">pop</span><span class="special">(</span><span class="identifier">tsk</span><span class="special">)){</span>
107                <span class="identifier">tsk</span><span class="special">();</span>
108            <span class="special">}</span>
109        <span class="special">});</span>
110<span class="comment">// feed channel with tasks</span>
111<span class="identifier">ch</span><span class="special">.</span><span class="identifier">push</span><span class="special">([]{</span> <span class="special">...</span> <span class="special">});</span>
112<span class="special">...</span>
113<span class="comment">// signal termination</span>
114<span class="identifier">ch</span><span class="special">.</span><span class="identifier">close</span><span class="special">();</span>
115<span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span>
116</pre>
117<p>
118      An alternative is to use a work-stealing scheduler. This kind of scheduling
119      algorithm a worker thread steals fibers from the ready-queue of other worker
120      threads if its own ready-queue is empty.
121    </p>
122<div class="note"><table border="0" summary="Note">
123<tr>
124<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
125<th align="left">Note</th>
126</tr>
127<tr><td align="left" valign="top"><p>
128        Wait till all worker threads have registered the work-stealing scheduling
129        algorithm.
130      </p></td></tr>
131</table></div>
132<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="identifier">mtx</span><span class="special">;</span>
133<span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">condition_variable_any</span> <span class="identifier">cv</span><span class="special">;</span>
134<span class="comment">// start wotrker-thread first</span>
135<span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span>
136        <span class="special">[&amp;</span><span class="identifier">mtx</span><span class="special">,&amp;</span><span class="identifier">cv</span><span class="special">]{</span>
137            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">work_stealing</span><span class="special">&gt;(</span><span class="number">2</span><span class="special">);</span>
138            <span class="identifier">mtx</span><span class="special">.</span><span class="identifier">lock</span><span class="special">();</span>
139            <span class="comment">// suspend main-fiber from the worker thread</span>
140            <span class="identifier">cv</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span><span class="identifier">mtx</span><span class="special">);</span>
141            <span class="identifier">mtx</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">();</span>
142        <span class="special">});</span>
143<span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">work_stealing</span><span class="special">&gt;(</span><span class="number">2</span><span class="special">);</span>
144<span class="comment">// create fibers with tasks</span>
145<span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f</span><span class="special">{[]{</span> <span class="special">...</span> <span class="special">}};</span>
146<span class="special">...</span>
147<span class="comment">// signal termination</span>
148<span class="identifier">cv</span><span class="special">.</span><span class="identifier">notify_all</span><span class="special">();</span>
149<span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span>
150</pre>
151<div class="important"><table border="0" summary="Important">
152<tr>
153<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td>
154<th align="left">Important</th>
155</tr>
156<tr><td align="left" valign="top"><p>
157        Because the TIB (thread information block on Windows) is not fully described
158        in the MSDN, it might be possible that not all required TIB-parts are swapped.
159        Using WinFiber implementation might be an alternative (see documentation
160        about <a href="http://www.boost.org/doc/libs/1_65_1/libs/context/doc/html/context/cc/implementations__fcontext_t__ucontext_t_and_winfiber.html" target="_top"><span class="emphasis"><em>implementations
161        fcontext_t, ucontext_t and WinFiber of boost.context</em></span></a>).
162      </p></td></tr>
163</table></div>
164</div>
165<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
166<td align="left"></td>
167<td align="right"><div class="copyright-footer">Copyright © 2013 Oliver Kowalke<p>
168        Distributed under the Boost Software License, Version 1.0. (See accompanying
169        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>)
170      </p>
171</div></td>
172</tr></table>
173<hr>
174<div class="spirit-nav">
175<a accesskey="p" href="gpu_computing/hip.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="performance.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
176</div>
177</body>
178</html>
179