• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Condition Variables</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="../synchronization.html" title="Synchronization">
9<link rel="prev" href="mutex_types.html" title="Mutex Types">
10<link rel="next" href="barriers.html" title="Barriers">
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="mutex_types.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../synchronization.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="barriers.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="fiber.synchronization.conditions"></a><a class="link" href="conditions.html" title="Condition Variables">Condition Variables</a>
28</h3></div></div></div>
29<h5>
30<a name="fiber.synchronization.conditions.h0"></a>
31        <span class="phrase"><a name="fiber.synchronization.conditions.synopsis"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.synopsis">Synopsis</a>
32      </h5>
33<pre class="programlisting"><span class="keyword">enum</span> <span class="keyword">class</span> <span class="identifier">cv_status</span><span class="special">;</span> <span class="special">{</span>
34    <span class="identifier">no_timeout</span><span class="special">,</span>
35    <span class="identifier">timeout</span>
36<span class="special">};</span>
37
38<span class="keyword">class</span> <span class="identifier">condition_variable</span><span class="special">;</span>
39<span class="keyword">class</span> <span class="identifier">condition_variable_any</span><span class="special">;</span>
40</pre>
41<p>
42        The class <a class="link" href="conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> provides a mechanism
43        for a fiber to wait for notification from another fiber. When the fiber awakens
44        from the wait, then it checks to see if the appropriate condition is now
45        true, and continues if so. If the condition is not true, then the fiber calls
46        <code class="computeroutput"><span class="identifier">wait</span></code> again to resume waiting.
47        In the simplest case, this condition is just a boolean variable:
48      </p>
49<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">condition_variable</span> <span class="identifier">cond</span><span class="special">;</span>
50<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>
51<span class="keyword">bool</span> <span class="identifier">data_ready</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span>
52
53<span class="keyword">void</span> <span class="identifier">process_data</span><span class="special">();</span>
54
55<span class="keyword">void</span> <span class="identifier">wait_for_data_to_process</span><span class="special">()</span> <span class="special">{</span>
56    <span class="special">{</span>
57        <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</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">mutex</span> <span class="special">&gt;</span> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx</span><span class="special">);</span>
58        <span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">data_ready</span><span class="special">)</span> <span class="special">{</span>
59            <span class="identifier">cond</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">);</span>
60        <span class="special">}</span>
61    <span class="special">}</span>   <span class="comment">// release lk</span>
62    <span class="identifier">process_data</span><span class="special">();</span>
63<span class="special">}</span>
64</pre>
65<p>
66        Notice that the <code class="computeroutput"><span class="identifier">lk</span></code> is passed
67        to <a class="link" href="conditions.html#condition_variable_wait"><code class="computeroutput">condition_variable::wait()</code></a>: <code class="computeroutput"><span class="identifier">wait</span><span class="special">()</span></code> will atomically add the fiber to the set
68        of fibers waiting on the condition variable, and unlock the <a class="link" href="mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a>.
69        When the fiber is awakened, the <code class="computeroutput"><span class="identifier">mutex</span></code>
70        will be locked again before the call to <code class="computeroutput"><span class="identifier">wait</span><span class="special">()</span></code> returns. This allows other fibers to acquire
71        the <code class="computeroutput"><span class="identifier">mutex</span></code> in order to update
72        the shared data, and ensures that the data associated with the condition
73        is correctly synchronized.
74      </p>
75<p>
76        <code class="computeroutput"><span class="identifier">wait_for_data_to_process</span><span class="special">()</span></code> could equivalently be written:
77      </p>
78<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">wait_for_data_to_process</span><span class="special">()</span> <span class="special">{</span>
79    <span class="special">{</span>
80        <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</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">mutex</span> <span class="special">&gt;</span> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx</span><span class="special">);</span>
81        <span class="comment">// make condition_variable::wait() perform the loop</span>
82        <span class="identifier">cond</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">data_ready</span><span class="special">;</span> <span class="special">});</span>
83    <span class="special">}</span>   <span class="comment">// release lk</span>
84    <span class="identifier">process_data</span><span class="special">();</span>
85<span class="special">}</span>
86</pre>
87<p>
88        In the meantime, another fiber sets <code class="computeroutput"><span class="identifier">data_ready</span></code>
89        to <code class="computeroutput"><span class="keyword">true</span></code>, and then calls either
90        <a class="link" href="conditions.html#condition_variable_notify_one"><code class="computeroutput">condition_variable::notify_one()</code></a> or <a class="link" href="conditions.html#condition_variable_notify_all"><code class="computeroutput">condition_variable::notify_all()</code></a> on
91        the <a class="link" href="conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> <code class="computeroutput"><span class="identifier">cond</span></code>
92        to wake one waiting fiber or all the waiting fibers respectively.
93      </p>
94<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">retrieve_data</span><span class="special">();</span>
95<span class="keyword">void</span> <span class="identifier">prepare_data</span><span class="special">();</span>
96
97<span class="keyword">void</span> <span class="identifier">prepare_data_for_processing</span><span class="special">()</span> <span class="special">{</span>
98    <span class="identifier">retrieve_data</span><span class="special">();</span>
99    <span class="identifier">prepare_data</span><span class="special">();</span>
100    <span class="special">{</span>
101        <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</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">mutex</span> <span class="special">&gt;</span> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx</span><span class="special">);</span>
102        <span class="identifier">data_ready</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span>
103    <span class="special">}</span>
104    <span class="identifier">cond</span><span class="special">.</span><span class="identifier">notify_one</span><span class="special">();</span>
105<span class="special">}</span>
106</pre>
107<p>
108        Note that the same <a class="link" href="mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a> is locked before the shared data is updated,
109        but that the <code class="computeroutput"><span class="identifier">mutex</span></code> does not
110        have to be locked across the call to <a class="link" href="conditions.html#condition_variable_notify_one"><code class="computeroutput">condition_variable::notify_one()</code></a>.
111      </p>
112<p>
113        Locking is important because the synchronization objects provided by <span class="bold"><strong>Boost.Fiber</strong></span> can be used to synchronize fibers running
114        on different threads.
115      </p>
116<p>
117        <span class="bold"><strong>Boost.Fiber</strong></span> provides both <a class="link" href="conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> and
118        <a class="link" href="conditions.html#class_condition_variable_any"><code class="computeroutput">condition_variable_any</code></a>. <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">condition_variable</span></code>
119        can only wait on <a href="http://en.cppreference.com/w/cpp/thread/unique_lock" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span></code></a><code class="computeroutput"><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span></code><a class="link" href="mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a><code class="computeroutput"> <span class="special">&gt;</span></code>
120        while <code class="computeroutput"><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></code> can wait on user-defined
121        lock types.
122      </p>
123<a name="condition_variable_spurious_wakeups"></a><h5>
124<a name="fiber.synchronization.conditions.h1"></a>
125        <span class="phrase"><a name="fiber.synchronization.conditions.no_spurious_wakeups"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.no_spurious_wakeups">No Spurious
126        Wakeups</a>
127      </h5>
128<p>
129        Neither <a class="link" href="conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> nor <a class="link" href="conditions.html#class_condition_variable_any"><code class="computeroutput">condition_variable_any</code></a> are
130        subject to spurious wakeup: <a class="link" href="conditions.html#condition_variable_wait"><code class="computeroutput">condition_variable::wait()</code></a> can
131        only wake up when <a class="link" href="conditions.html#condition_variable_notify_one"><code class="computeroutput">condition_variable::notify_one()</code></a> or
132        <a class="link" href="conditions.html#condition_variable_notify_all"><code class="computeroutput">condition_variable::notify_all()</code></a> is called. Even
133        so, it is prudent to use one of the <code class="computeroutput"><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lock</span><span class="special">,</span> <span class="identifier">predicate</span> <span class="special">)</span></code> overloads.
134      </p>
135<p>
136        Consider a set of consumer fibers processing items from a <a href="http://en.cppreference.com/w/cpp/container/queue" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">queue</span></code></a>.
137        The queue is continually populated by a set of producer fibers.
138      </p>
139<p>
140        The consumer fibers might reasonably wait on a <code class="computeroutput"><span class="identifier">condition_variable</span></code>
141        as long as the queue remains <a href="http://en.cppreference.com/w/cpp/container/queue/empty" target="_top"><code class="computeroutput"><span class="identifier">empty</span><span class="special">()</span></code></a>.
142      </p>
143<p>
144        Because producer fibers might <a href="http://en.cppreference.com/w/cpp/container/queue/push" target="_top"><code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code></a>
145        items to the queue in bursts, they call <a class="link" href="conditions.html#condition_variable_notify_all"><code class="computeroutput">condition_variable::notify_all()</code></a> rather
146        than <a class="link" href="conditions.html#condition_variable_notify_one"><code class="computeroutput">condition_variable::notify_one()</code></a>.
147      </p>
148<p>
149        But a given consumer fiber might well wake up from <a class="link" href="conditions.html#condition_variable_wait"><code class="computeroutput">condition_variable::wait()</code></a> and
150        find the queue <code class="computeroutput"><span class="identifier">empty</span><span class="special">()</span></code>,
151        because other consumer fibers might already have processed all pending items.
152      </p>
153<p>
154        (See also <a class="link" href="../rationale.html#spurious_wakeup">spurious wakeup</a>.)
155      </p>
156<a name="class_cv_status"></a><h5>
157<a name="fiber.synchronization.conditions.h2"></a>
158        <span class="phrase"><a name="fiber.synchronization.conditions.enumeration__code__phrase_role__identifier__cv_status__phrase___code_"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.enumeration__code__phrase_role__identifier__cv_status__phrase___code_">Enumeration
159        <code class="computeroutput"><span class="identifier">cv_status</span></code></a>
160      </h5>
161<p>
162        A timed wait operation might return because of timeout or not.
163      </p>
164<pre class="programlisting"><span class="keyword">enum</span> <span class="keyword">class</span> <span class="identifier">cv_status</span> <span class="special">{</span>
165    <span class="identifier">no_timeout</span><span class="special">,</span>
166    <span class="identifier">timeout</span>
167<span class="special">};</span>
168</pre>
169<h5>
170<a name="fiber.synchronization.conditions.h3"></a>
171        <span class="phrase"><a name="fiber.synchronization.conditions._code__phrase_role__identifier__no_timeout__phrase___code_"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions._code__phrase_role__identifier__no_timeout__phrase___code_"><code class="computeroutput"><span class="identifier">no_timeout</span></code></a>
172      </h5>
173<div class="variablelist">
174<p class="title"><b></b></p>
175<dl class="variablelist">
176<dt><span class="term">Effects:</span></dt>
177<dd><p>
178              The condition variable was awakened with <code class="computeroutput"><span class="identifier">notify_one</span></code>
179              or <code class="computeroutput"><span class="identifier">notify_all</span></code>.
180            </p></dd>
181</dl>
182</div>
183<h5>
184<a name="fiber.synchronization.conditions.h4"></a>
185        <span class="phrase"><a name="fiber.synchronization.conditions._code__phrase_role__identifier__timeout__phrase___code_"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions._code__phrase_role__identifier__timeout__phrase___code_"><code class="computeroutput"><span class="identifier">timeout</span></code></a>
186      </h5>
187<div class="variablelist">
188<p class="title"><b></b></p>
189<dl class="variablelist">
190<dt><span class="term">Effects:</span></dt>
191<dd><p>
192              The condition variable was awakened by timeout.
193            </p></dd>
194</dl>
195</div>
196<p>
197        </p>
198<h5>
199<a name="class_condition_variable_any_bridgehead"></a>
200  <span class="phrase"><a name="class_condition_variable_any"></a></span>
201  <a class="link" href="conditions.html#class_condition_variable_any">Class
202        <code class="computeroutput">condition_variable_any</code></a>
203</h5>
204<p>
205      </p>
206<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">condition_variable</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
207
208<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
209<span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span>
210
211<span class="keyword">class</span> condition_variable_any <span class="special">{</span>
212<span class="keyword">public</span><span class="special">:</span>
213    condition_variable_any<span class="special">();</span>
214    <span class="special">~</span>condition_variable_any<span class="special">();</span>
215
216    condition_variable_any<span class="special">(</span> condition_variable_any <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span>
217    condition_variable_any <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> condition_variable_any <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span>
218
219    <span class="keyword">void</span> <span class="identifier">notify_one</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span>
220    <span class="keyword">void</span> <span class="identifier">notify_all</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span>
221
222    template&lt; typename LockType &gt;
223    void <span class="identifier">wait</span><span class="special">(</span> LockType <span class="special">&amp;);</span>
224
225    <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Pred</span> <span class="special">&gt;</span>
226    <span class="keyword">void</span> <span class="identifier">wait</span><span class="special">(</span> LockType <span class="special">&amp;,</span> <span class="identifier">Pred</span><span class="special">);</span>
227
228    <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span>
229    <span class="identifier">cv_status</span> <span class="identifier">wait_until</span><span class="special">(</span> LockType <span class="special">&amp;,</span>
230                          <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span>
231
232    <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span>
233    <span class="keyword">bool</span> <span class="identifier">wait_until</span><span class="special">(</span> LockType <span class="special">&amp;,</span>
234                     <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;,</span>
235                     <span class="identifier">Pred</span><span class="special">);</span>
236
237    <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span>
238    <span class="identifier">cv_status</span> <span class="identifier">wait_for</span><span class="special">(</span> LockType <span class="special">&amp;,</span>
239                        <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span>
240
241    <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span>
242    <span class="keyword">bool</span> <span class="identifier">wait_for</span><span class="special">(</span> LockType <span class="special">&amp;,</span>
243                   <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;,</span>
244                   <span class="identifier">Pred</span><span class="special">);</span>
245<span class="special">};</span>
246
247<span class="special">}}</span>
248</pre>
249<h5>
250<a name="fiber.synchronization.conditions.h5"></a>
251        <span class="phrase"><a name="fiber.synchronization.conditions.constructor"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.constructor">Constructor</a>
252      </h5>
253<pre class="programlisting">condition_variable_any<span class="special">()</span>
254</pre>
255<div class="variablelist">
256<p class="title"><b></b></p>
257<dl class="variablelist">
258<dt><span class="term">Effects:</span></dt>
259<dd><p>
260              Creates the object.
261            </p></dd>
262<dt><span class="term">Throws:</span></dt>
263<dd><p>
264              Nothing.
265            </p></dd>
266</dl>
267</div>
268<h5>
269<a name="fiber.synchronization.conditions.h6"></a>
270        <span class="phrase"><a name="fiber.synchronization.conditions.destructor"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.destructor">Destructor</a>
271      </h5>
272<pre class="programlisting"><span class="special">~</span>condition_variable_any<span class="special">()</span>
273</pre>
274<div class="variablelist">
275<p class="title"><b></b></p>
276<dl class="variablelist">
277<dt><span class="term">Precondition:</span></dt>
278<dd><p>
279              All fibers waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> have been notified by a call to
280              <code class="computeroutput"><span class="identifier">notify_one</span></code> or <code class="computeroutput"><span class="identifier">notify_all</span></code> (though the respective
281              calls to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>
282              need not have returned).
283            </p></dd>
284<dt><span class="term">Effects:</span></dt>
285<dd><p>
286              Destroys the object.
287            </p></dd>
288</dl>
289</div>
290<p>
291        </p>
292<h5>
293<a name="condition_variable_any_notify_one_bridgehead"></a>
294  <span class="phrase"><a name="condition_variable_any_notify_one"></a></span>
295  <a class="link" href="conditions.html#condition_variable_any_notify_one">Member
296        function <code class="computeroutput">notify_one</code>()</a>
297</h5>
298<p>
299      </p>
300<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">notify_one</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span>
301</pre>
302<div class="variablelist">
303<p class="title"><b></b></p>
304<dl class="variablelist">
305<dt><span class="term">Effects:</span></dt>
306<dd><p>
307              If any fibers are currently <a class="link" href="../overview.html#blocking"><span class="emphasis"><em>blocked</em></span></a>
308              waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
309              in a call to <code class="computeroutput"><span class="identifier">wait</span></code>,
310              <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>, unblocks one of those
311              fibers.
312            </p></dd>
313<dt><span class="term">Throws:</span></dt>
314<dd><p>
315              Nothing.
316            </p></dd>
317<dt><span class="term">Note:</span></dt>
318<dd><p>
319              It is arbitrary which waiting fiber is resumed.
320            </p></dd>
321</dl>
322</div>
323<p>
324        </p>
325<h5>
326<a name="condition_variable_any_notify_all_bridgehead"></a>
327  <span class="phrase"><a name="condition_variable_any_notify_all"></a></span>
328  <a class="link" href="conditions.html#condition_variable_any_notify_all">Member
329        function <code class="computeroutput">notify_all</code>()</a>
330</h5>
331<p>
332      </p>
333<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">notify_all</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span>
334</pre>
335<div class="variablelist">
336<p class="title"><b></b></p>
337<dl class="variablelist">
338<dt><span class="term">Effects:</span></dt>
339<dd><p>
340              If any fibers are currently <a class="link" href="../overview.html#blocking"><span class="emphasis"><em>blocked</em></span></a>
341              waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
342              in a call to <code class="computeroutput"><span class="identifier">wait</span></code>,
343              <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>, unblocks all of those
344              fibers.
345            </p></dd>
346<dt><span class="term">Throws:</span></dt>
347<dd><p>
348              Nothing.
349            </p></dd>
350<dt><span class="term">Note:</span></dt>
351<dd><p>
352              This is why a waiting fiber must <span class="emphasis"><em>also</em></span> check for
353              the desired program state using a mechanism external to the <code class="computeroutput">condition_variable_any</code>,
354              and retry the wait until that state is reached. A fiber waiting on
355              a <code class="computeroutput">condition_variable_any</code> might well wake up a number of times before
356              the desired state is reached.
357            </p></dd>
358</dl>
359</div>
360<p>
361        </p>
362<h5>
363<a name="condition_variable_any_wait_bridgehead"></a>
364  <span class="phrase"><a name="condition_variable_any_wait"></a></span>
365  <a class="link" href="conditions.html#condition_variable_any_wait">Templated
366        member function <code class="computeroutput">wait</code>()</a>
367</h5>
368<p>
369      </p>
370<pre class="programlisting">template&lt; typename LockType &gt;
371    void <span class="identifier">wait</span><span class="special">(</span> LockType <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">);</span>
372
373<span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Pred</span> <span class="special">&gt;</span>
374<span class="keyword">void</span> <span class="identifier">wait</span><span class="special">(</span> LockType <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">Pred</span> <span class="identifier">pred</span><span class="special">);</span>
375</pre>
376<div class="variablelist">
377<p class="title"><b></b></p>
378<dl class="variablelist">
379<dt><span class="term">Precondition:</span></dt>
380<dd><p>
381              <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the
382              current fiber, and either no other fiber is currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>,
383              or the execution of the <a href="http://en.cppreference.com/w/cpp/thread/unique_lock/mutex" target="_top"><code class="computeroutput"><span class="identifier">mutex</span><span class="special">()</span></code></a>
384              member function on the <code class="computeroutput"><span class="identifier">lk</span></code>
385              objects supplied in the calls to <code class="computeroutput"><span class="identifier">wait</span></code>
386              in all the fibers currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> would return the same value as
387              <code class="computeroutput"><span class="identifier">lk</span><span class="special">-&gt;</span><span class="identifier">mutex</span><span class="special">()</span></code>
388              for this call to <code class="computeroutput"><span class="identifier">wait</span></code>.
389            </p></dd>
390<dt><span class="term">Effects:</span></dt>
391<dd>
392<p>
393              Atomically call <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">()</span></code> and blocks the current fiber. The
394              fiber will unblock when notified by a call to <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_all</span><span class="special">()</span></code>. When the fiber is unblocked (for
395              whatever reason), the lock is reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code>
396              before the call to <code class="computeroutput"><span class="identifier">wait</span></code>
397              returns. The lock is also reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> if the function exits with an exception.
398              The member function accepting <code class="computeroutput"><span class="identifier">pred</span></code>
399              is shorthand for:
400</p>
401<pre class="programlisting"><span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">pred</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span>
402    <span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">);</span>
403<span class="special">}</span>
404</pre>
405<p>
406            </p>
407</dd>
408<dt><span class="term">Postcondition:</span></dt>
409<dd><p>
410              <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the
411              current fiber.
412            </p></dd>
413<dt><span class="term">Throws:</span></dt>
414<dd><p>
415              <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error
416              occurs.
417            </p></dd>
418<dt><span class="term">Note:</span></dt>
419<dd><p>
420              The Precondition is a bit dense. It merely states that all the fibers
421              concurrently calling <code class="computeroutput"><span class="identifier">wait</span></code>
422              on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
423              must wait on <code class="computeroutput"><span class="identifier">lk</span></code> objects
424              governing the <span class="emphasis"><em>same</em></span> <a class="link" href="mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a>. Three distinct
425              objects are involved in any <code class="computeroutput">condition_variable_any::wait()</code> call: the
426              <code class="computeroutput">condition_variable_any</code> itself, the <code class="computeroutput"><span class="identifier">mutex</span></code>
427              coordinating access between fibers and a local lock object (e.g. <a href="http://en.cppreference.com/w/cpp/thread/unique_lock" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span></code></a>). In general,
428              you can partition the lifespan of a given <code class="computeroutput">condition_variable_any</code> instance
429              into periods with one or more fibers waiting on it, separated by periods
430              when no fibers are waiting on it. When more than one fiber is waiting
431              on that <code class="computeroutput">condition_variable_any</code>, all must pass lock objects referencing
432              the <span class="emphasis"><em>same</em></span> <code class="computeroutput"><span class="identifier">mutex</span></code>
433              instance.
434            </p></dd>
435</dl>
436</div>
437<p>
438        </p>
439<h5>
440<a name="condition_variable_any_wait_until_bridgehead"></a>
441  <span class="phrase"><a name="condition_variable_any_wait_until"></a></span>
442  <a class="link" href="conditions.html#condition_variable_any_wait_until">Templated
443        member function <code class="computeroutput">wait_until</code>()</a>
444</h5>
445<p>
446      </p>
447<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span>
448<span class="identifier">cv_status</span> <span class="identifier">wait_until</span><span class="special">(</span> LockType <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span>
449                      <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">);</span>
450
451<span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span>
452<span class="keyword">bool</span> <span class="identifier">wait_until</span><span class="special">(</span> LockType <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span>
453                 <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">,</span>
454                 <span class="identifier">Pred</span> <span class="identifier">pred</span><span class="special">);</span>
455</pre>
456<div class="variablelist">
457<p class="title"><b></b></p>
458<dl class="variablelist">
459<dt><span class="term">Precondition:</span></dt>
460<dd><p>
461              <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the
462              current fiber, and either no other fiber is currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>,
463              or the execution of the <code class="computeroutput"><span class="identifier">mutex</span><span class="special">()</span></code> member function on the <code class="computeroutput"><span class="identifier">lk</span></code> objects supplied in the calls
464              to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>
465              in all the fibers currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> would return the same value as
466              <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">mutex</span><span class="special">()</span></code>
467              for this call to <code class="computeroutput"><span class="identifier">wait_until</span></code>.
468            </p></dd>
469<dt><span class="term">Effects:</span></dt>
470<dd>
471<p>
472              Atomically call <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">()</span></code> and blocks the current fiber. The
473              fiber will unblock when notified by a call to <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_all</span><span class="special">()</span></code>, when the system time would be equal
474              to or later than the specified <code class="computeroutput"><span class="identifier">abs_time</span></code>.
475              When the fiber is unblocked (for whatever reason), the lock is reacquired
476              by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code>
477              before the call to <code class="computeroutput"><span class="identifier">wait_until</span></code>
478              returns. The lock is also reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> if the function exits with an exception.
479              The member function accepting <code class="computeroutput"><span class="identifier">pred</span></code>
480              is shorthand for:
481</p>
482<pre class="programlisting"><span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">pred</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span>
483    <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span> <span class="special">==</span> <span class="identifier">wait_until</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="special">)</span>
484        <span class="keyword">return</span> <span class="identifier">pred</span><span class="special">();</span>
485<span class="special">}</span>
486<span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
487</pre>
488<p>
489              That is, even if <code class="computeroutput"><span class="identifier">wait_until</span><span class="special">()</span></code> times out, it can still return <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="identifier">pred</span><span class="special">()</span></code> returns <code class="computeroutput"><span class="keyword">true</span></code>
490              at that time.
491            </p>
492</dd>
493<dt><span class="term">Postcondition:</span></dt>
494<dd><p>
495              <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the
496              current fiber.
497            </p></dd>
498<dt><span class="term">Throws:</span></dt>
499<dd><p>
500              <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error
501              occurs or timeout-related exceptions.
502            </p></dd>
503<dt><span class="term">Returns:</span></dt>
504<dd><p>
505              The overload without <code class="computeroutput"><span class="identifier">pred</span></code>
506              returns <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">no_timeout</span></code> if awakened by <code class="computeroutput"><span class="identifier">notify_one</span><span class="special">()</span></code>
507              or <code class="computeroutput"><span class="identifier">notify_all</span><span class="special">()</span></code>,
508              or <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span></code> if awakened because the system
509              time is past <code class="computeroutput"><span class="identifier">abs_time</span></code>.
510            </p></dd>
511<dt><span class="term">Returns:</span></dt>
512<dd><p>
513              The overload accepting <code class="computeroutput"><span class="identifier">pred</span></code>
514              returns <code class="computeroutput"><span class="keyword">false</span></code> if the call
515              is returning because the time specified by <code class="computeroutput"><span class="identifier">abs_time</span></code>
516              was reached and the predicate returns <code class="computeroutput"><span class="keyword">false</span></code>,
517              <code class="computeroutput"><span class="keyword">true</span></code> otherwise.
518            </p></dd>
519<dt><span class="term">Note:</span></dt>
520<dd><p>
521              See <span class="bold"><strong>Note</strong></span> for <a class="link" href="conditions.html#condition_variable_any_wait"><code class="computeroutput">condition_variable_any::wait()</code></a>.
522            </p></dd>
523</dl>
524</div>
525<p>
526        </p>
527<h5>
528<a name="condition_variable_any_wait_for_bridgehead"></a>
529  <span class="phrase"><a name="condition_variable_any_wait_for"></a></span>
530  <a class="link" href="conditions.html#condition_variable_any_wait_for">Templated
531        member function <code class="computeroutput">wait_for</code>()</a>
532</h5>
533<p>
534      </p>
535<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span>
536<span class="identifier">cv_status</span> <span class="identifier">wait_for</span><span class="special">(</span> LockType <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span>
537                    <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rel_time</span><span class="special">);</span>
538
539<span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span>
540<span class="keyword">bool</span> <span class="identifier">wait_for</span><span class="special">(</span> LockType <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span>
541               <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rel_time</span><span class="special">,</span>
542               <span class="identifier">Pred</span> <span class="identifier">pred</span><span class="special">);</span>
543</pre>
544<div class="variablelist">
545<p class="title"><b></b></p>
546<dl class="variablelist">
547<dt><span class="term">Precondition:</span></dt>
548<dd><p>
549              <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the
550              current fiber, and either no other fiber is currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>,
551              or the execution of the <code class="computeroutput"><span class="identifier">mutex</span><span class="special">()</span></code> member function on the <code class="computeroutput"><span class="identifier">lk</span></code> objects supplied in the calls
552              to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>
553              in all the fibers currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> would return the same value as
554              <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">mutex</span><span class="special">()</span></code>
555              for this call to <code class="computeroutput"><span class="identifier">wait_for</span></code>.
556            </p></dd>
557<dt><span class="term">Effects:</span></dt>
558<dd>
559<p>
560              Atomically call <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">()</span></code> and blocks the current fiber. The
561              fiber will unblock when notified by a call to <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_all</span><span class="special">()</span></code>, when a time interval equal to or
562              greater than the specified <code class="computeroutput"><span class="identifier">rel_time</span></code>
563              has elapsed. When the fiber is unblocked (for whatever reason), the
564              lock is reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> before the call to <code class="computeroutput"><span class="identifier">wait</span></code> returns. The lock is also reacquired
565              by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code>
566              if the function exits with an exception. The <code class="computeroutput"><span class="identifier">wait_for</span><span class="special">()</span></code> member function accepting <code class="computeroutput"><span class="identifier">pred</span></code> is shorthand for:
567</p>
568<pre class="programlisting"><span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">pred</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span>
569    <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span> <span class="special">==</span> <span class="identifier">wait_for</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">rel_time</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span>
570        <span class="keyword">return</span> <span class="identifier">pred</span><span class="special">();</span>
571    <span class="special">}</span>
572<span class="special">}</span>
573<span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
574</pre>
575<p>
576              (except of course that <code class="computeroutput"><span class="identifier">rel_time</span></code>
577              is adjusted for each iteration). The point is that, even if <code class="computeroutput"><span class="identifier">wait_for</span><span class="special">()</span></code>
578              times out, it can still return <code class="computeroutput"><span class="keyword">true</span></code>
579              if <code class="computeroutput"><span class="identifier">pred</span><span class="special">()</span></code>
580              returns <code class="computeroutput"><span class="keyword">true</span></code> at that time.
581            </p>
582</dd>
583<dt><span class="term">Postcondition:</span></dt>
584<dd><p>
585              <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the
586              current fiber.
587            </p></dd>
588<dt><span class="term">Throws:</span></dt>
589<dd><p>
590              <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error
591              occurs or timeout-related exceptions.
592            </p></dd>
593<dt><span class="term">Returns:</span></dt>
594<dd><p>
595              The overload without <code class="computeroutput"><span class="identifier">pred</span></code>
596              returns <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">no_timeout</span></code> if awakened by <code class="computeroutput"><span class="identifier">notify_one</span><span class="special">()</span></code>
597              or <code class="computeroutput"><span class="identifier">notify_all</span><span class="special">()</span></code>,
598              or <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span></code> if awakened because at least
599              <code class="computeroutput"><span class="identifier">rel_time</span></code> has elapsed.
600            </p></dd>
601<dt><span class="term">Returns:</span></dt>
602<dd><p>
603              The overload accepting <code class="computeroutput"><span class="identifier">pred</span></code>
604              returns <code class="computeroutput"><span class="keyword">false</span></code> if the call
605              is returning because at least <code class="computeroutput"><span class="identifier">rel_time</span></code>
606              has elapsed and the predicate returns <code class="computeroutput"><span class="keyword">false</span></code>,
607              <code class="computeroutput"><span class="keyword">true</span></code> otherwise.
608            </p></dd>
609<dt><span class="term">Note:</span></dt>
610<dd><p>
611              See <span class="bold"><strong>Note</strong></span> for <a class="link" href="conditions.html#condition_variable_any_wait"><code class="computeroutput">condition_variable_any::wait()</code></a>.
612            </p></dd>
613</dl>
614</div>
615<p>
616        </p>
617<h5>
618<a name="class_condition_variable_bridgehead"></a>
619  <span class="phrase"><a name="class_condition_variable"></a></span>
620  <a class="link" href="conditions.html#class_condition_variable">Class
621        <code class="computeroutput">condition_variable</code></a>
622</h5>
623<p>
624      </p>
625<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">condition_variable</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
626
627<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
628<span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span>
629
630<span class="keyword">class</span> condition_variable <span class="special">{</span>
631<span class="keyword">public</span><span class="special">:</span>
632    condition_variable<span class="special">();</span>
633    <span class="special">~</span>condition_variable<span class="special">();</span>
634
635    condition_variable<span class="special">(</span> condition_variable <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span>
636    condition_variable <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> condition_variable <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span>
637
638    <span class="keyword">void</span> <span class="identifier">notify_one</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span>
639    <span class="keyword">void</span> <span class="identifier">notify_all</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span>
640
641    void <span class="identifier">wait</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;);</span>
642
643    <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Pred</span> <span class="special">&gt;</span>
644    <span class="keyword">void</span> <span class="identifier">wait</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;,</span> <span class="identifier">Pred</span><span class="special">);</span>
645
646    <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span>
647    <span class="identifier">cv_status</span> <span class="identifier">wait_until</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;,</span>
648                          <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span>
649
650    <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span>
651    <span class="keyword">bool</span> <span class="identifier">wait_until</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;,</span>
652                     <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;,</span>
653                     <span class="identifier">Pred</span><span class="special">);</span>
654
655    <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span>
656    <span class="identifier">cv_status</span> <span class="identifier">wait_for</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;,</span>
657                        <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span>
658
659    <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span>
660    <span class="keyword">bool</span> <span class="identifier">wait_for</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;,</span>
661                   <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;,</span>
662                   <span class="identifier">Pred</span><span class="special">);</span>
663<span class="special">};</span>
664
665<span class="special">}}</span>
666</pre>
667<h5>
668<a name="fiber.synchronization.conditions.h7"></a>
669        <span class="phrase"><a name="fiber.synchronization.conditions.constructor0"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.constructor0">Constructor</a>
670      </h5>
671<pre class="programlisting">condition_variable<span class="special">()</span>
672</pre>
673<div class="variablelist">
674<p class="title"><b></b></p>
675<dl class="variablelist">
676<dt><span class="term">Effects:</span></dt>
677<dd><p>
678              Creates the object.
679            </p></dd>
680<dt><span class="term">Throws:</span></dt>
681<dd><p>
682              Nothing.
683            </p></dd>
684</dl>
685</div>
686<h5>
687<a name="fiber.synchronization.conditions.h8"></a>
688        <span class="phrase"><a name="fiber.synchronization.conditions.destructor0"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.destructor0">Destructor</a>
689      </h5>
690<pre class="programlisting"><span class="special">~</span>condition_variable<span class="special">()</span>
691</pre>
692<div class="variablelist">
693<p class="title"><b></b></p>
694<dl class="variablelist">
695<dt><span class="term">Precondition:</span></dt>
696<dd><p>
697              All fibers waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> have been notified by a call to
698              <code class="computeroutput"><span class="identifier">notify_one</span></code> or <code class="computeroutput"><span class="identifier">notify_all</span></code> (though the respective
699              calls to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>
700              need not have returned).
701            </p></dd>
702<dt><span class="term">Effects:</span></dt>
703<dd><p>
704              Destroys the object.
705            </p></dd>
706</dl>
707</div>
708<p>
709        </p>
710<h5>
711<a name="condition_variable_notify_one_bridgehead"></a>
712  <span class="phrase"><a name="condition_variable_notify_one"></a></span>
713  <a class="link" href="conditions.html#condition_variable_notify_one">Member
714        function <code class="computeroutput">notify_one</code>()</a>
715</h5>
716<p>
717      </p>
718<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">notify_one</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span>
719</pre>
720<div class="variablelist">
721<p class="title"><b></b></p>
722<dl class="variablelist">
723<dt><span class="term">Effects:</span></dt>
724<dd><p>
725              If any fibers are currently <a class="link" href="../overview.html#blocking"><span class="emphasis"><em>blocked</em></span></a>
726              waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
727              in a call to <code class="computeroutput"><span class="identifier">wait</span></code>,
728              <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>, unblocks one of those
729              fibers.
730            </p></dd>
731<dt><span class="term">Throws:</span></dt>
732<dd><p>
733              Nothing.
734            </p></dd>
735<dt><span class="term">Note:</span></dt>
736<dd><p>
737              It is arbitrary which waiting fiber is resumed.
738            </p></dd>
739</dl>
740</div>
741<p>
742        </p>
743<h5>
744<a name="condition_variable_notify_all_bridgehead"></a>
745  <span class="phrase"><a name="condition_variable_notify_all"></a></span>
746  <a class="link" href="conditions.html#condition_variable_notify_all">Member
747        function <code class="computeroutput">notify_all</code>()</a>
748</h5>
749<p>
750      </p>
751<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">notify_all</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span>
752</pre>
753<div class="variablelist">
754<p class="title"><b></b></p>
755<dl class="variablelist">
756<dt><span class="term">Effects:</span></dt>
757<dd><p>
758              If any fibers are currently <a class="link" href="../overview.html#blocking"><span class="emphasis"><em>blocked</em></span></a>
759              waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
760              in a call to <code class="computeroutput"><span class="identifier">wait</span></code>,
761              <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>, unblocks all of those
762              fibers.
763            </p></dd>
764<dt><span class="term">Throws:</span></dt>
765<dd><p>
766              Nothing.
767            </p></dd>
768<dt><span class="term">Note:</span></dt>
769<dd><p>
770              This is why a waiting fiber must <span class="emphasis"><em>also</em></span> check for
771              the desired program state using a mechanism external to the <code class="computeroutput">condition_variable</code>,
772              and retry the wait until that state is reached. A fiber waiting on
773              a <code class="computeroutput">condition_variable</code> might well wake up a number of times before the
774              desired state is reached.
775            </p></dd>
776</dl>
777</div>
778<p>
779        </p>
780<h5>
781<a name="condition_variable_wait_bridgehead"></a>
782  <span class="phrase"><a name="condition_variable_wait"></a></span>
783  <a class="link" href="conditions.html#condition_variable_wait">Templated
784        member function <code class="computeroutput">wait</code>()</a>
785</h5>
786<p>
787      </p>
788<pre class="programlisting">void <span class="identifier">wait</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">);</span>
789
790<span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Pred</span> <span class="special">&gt;</span>
791<span class="keyword">void</span> <span class="identifier">wait</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">Pred</span> <span class="identifier">pred</span><span class="special">);</span>
792</pre>
793<div class="variablelist">
794<p class="title"><b></b></p>
795<dl class="variablelist">
796<dt><span class="term">Precondition:</span></dt>
797<dd><p>
798              <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the
799              current fiber, and either no other fiber is currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>,
800              or the execution of the <a href="http://en.cppreference.com/w/cpp/thread/unique_lock/mutex" target="_top"><code class="computeroutput"><span class="identifier">mutex</span><span class="special">()</span></code></a>
801              member function on the <code class="computeroutput"><span class="identifier">lk</span></code>
802              objects supplied in the calls to <code class="computeroutput"><span class="identifier">wait</span></code>
803              in all the fibers currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> would return the same value as
804              <code class="computeroutput"><span class="identifier">lk</span><span class="special">-&gt;</span><span class="identifier">mutex</span><span class="special">()</span></code>
805              for this call to <code class="computeroutput"><span class="identifier">wait</span></code>.
806            </p></dd>
807<dt><span class="term">Effects:</span></dt>
808<dd>
809<p>
810              Atomically call <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">()</span></code> and blocks the current fiber. The
811              fiber will unblock when notified by a call to <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_all</span><span class="special">()</span></code>. When the fiber is unblocked (for
812              whatever reason), the lock is reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code>
813              before the call to <code class="computeroutput"><span class="identifier">wait</span></code>
814              returns. The lock is also reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> if the function exits with an exception.
815              The member function accepting <code class="computeroutput"><span class="identifier">pred</span></code>
816              is shorthand for:
817</p>
818<pre class="programlisting"><span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">pred</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span>
819    <span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">);</span>
820<span class="special">}</span>
821</pre>
822<p>
823            </p>
824</dd>
825<dt><span class="term">Postcondition:</span></dt>
826<dd><p>
827              <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the
828              current fiber.
829            </p></dd>
830<dt><span class="term">Throws:</span></dt>
831<dd><p>
832              <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error
833              occurs.
834            </p></dd>
835<dt><span class="term">Note:</span></dt>
836<dd><p>
837              The Precondition is a bit dense. It merely states that all the fibers
838              concurrently calling <code class="computeroutput"><span class="identifier">wait</span></code>
839              on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
840              must wait on <code class="computeroutput"><span class="identifier">lk</span></code> objects
841              governing the <span class="emphasis"><em>same</em></span> <a class="link" href="mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a>. Three distinct
842              objects are involved in any <code class="computeroutput">condition_variable::wait()</code> call: the <code class="computeroutput">condition_variable</code> itself,
843              the <code class="computeroutput"><span class="identifier">mutex</span></code> coordinating
844              access between fibers and a local lock object (e.g. <a href="http://en.cppreference.com/w/cpp/thread/unique_lock" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span></code></a>). In general,
845              you can partition the lifespan of a given <code class="computeroutput">condition_variable</code> instance
846              into periods with one or more fibers waiting on it, separated by periods
847              when no fibers are waiting on it. When more than one fiber is waiting
848              on that <code class="computeroutput">condition_variable</code>, all must pass lock objects referencing
849              the <span class="emphasis"><em>same</em></span> <code class="computeroutput"><span class="identifier">mutex</span></code>
850              instance.
851            </p></dd>
852</dl>
853</div>
854<p>
855        </p>
856<h5>
857<a name="condition_variable_wait_until_bridgehead"></a>
858  <span class="phrase"><a name="condition_variable_wait_until"></a></span>
859  <a class="link" href="conditions.html#condition_variable_wait_until">Templated
860        member function <code class="computeroutput">wait_until</code>()</a>
861</h5>
862<p>
863      </p>
864<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span>
865<span class="identifier">cv_status</span> <span class="identifier">wait_until</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span>
866                      <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">);</span>
867
868<span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span>
869<span class="keyword">bool</span> <span class="identifier">wait_until</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span>
870                 <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">,</span>
871                 <span class="identifier">Pred</span> <span class="identifier">pred</span><span class="special">);</span>
872</pre>
873<div class="variablelist">
874<p class="title"><b></b></p>
875<dl class="variablelist">
876<dt><span class="term">Precondition:</span></dt>
877<dd><p>
878              <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the
879              current fiber, and either no other fiber is currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>,
880              or the execution of the <code class="computeroutput"><span class="identifier">mutex</span><span class="special">()</span></code> member function on the <code class="computeroutput"><span class="identifier">lk</span></code> objects supplied in the calls
881              to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>
882              in all the fibers currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> would return the same value as
883              <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">mutex</span><span class="special">()</span></code>
884              for this call to <code class="computeroutput"><span class="identifier">wait_until</span></code>.
885            </p></dd>
886<dt><span class="term">Effects:</span></dt>
887<dd>
888<p>
889              Atomically call <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">()</span></code> and blocks the current fiber. The
890              fiber will unblock when notified by a call to <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_all</span><span class="special">()</span></code>, when the system time would be equal
891              to or later than the specified <code class="computeroutput"><span class="identifier">abs_time</span></code>.
892              When the fiber is unblocked (for whatever reason), the lock is reacquired
893              by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code>
894              before the call to <code class="computeroutput"><span class="identifier">wait_until</span></code>
895              returns. The lock is also reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> if the function exits with an exception.
896              The member function accepting <code class="computeroutput"><span class="identifier">pred</span></code>
897              is shorthand for:
898</p>
899<pre class="programlisting"><span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">pred</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span>
900    <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span> <span class="special">==</span> <span class="identifier">wait_until</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="special">)</span>
901        <span class="keyword">return</span> <span class="identifier">pred</span><span class="special">();</span>
902<span class="special">}</span>
903<span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
904</pre>
905<p>
906              That is, even if <code class="computeroutput"><span class="identifier">wait_until</span><span class="special">()</span></code> times out, it can still return <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="identifier">pred</span><span class="special">()</span></code> returns <code class="computeroutput"><span class="keyword">true</span></code>
907              at that time.
908            </p>
909</dd>
910<dt><span class="term">Postcondition:</span></dt>
911<dd><p>
912              <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the
913              current fiber.
914            </p></dd>
915<dt><span class="term">Throws:</span></dt>
916<dd><p>
917              <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error
918              occurs or timeout-related exceptions.
919            </p></dd>
920<dt><span class="term">Returns:</span></dt>
921<dd><p>
922              The overload without <code class="computeroutput"><span class="identifier">pred</span></code>
923              returns <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">no_timeout</span></code> if awakened by <code class="computeroutput"><span class="identifier">notify_one</span><span class="special">()</span></code>
924              or <code class="computeroutput"><span class="identifier">notify_all</span><span class="special">()</span></code>,
925              or <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span></code> if awakened because the system
926              time is past <code class="computeroutput"><span class="identifier">abs_time</span></code>.
927            </p></dd>
928<dt><span class="term">Returns:</span></dt>
929<dd><p>
930              The overload accepting <code class="computeroutput"><span class="identifier">pred</span></code>
931              returns <code class="computeroutput"><span class="keyword">false</span></code> if the call
932              is returning because the time specified by <code class="computeroutput"><span class="identifier">abs_time</span></code>
933              was reached and the predicate returns <code class="computeroutput"><span class="keyword">false</span></code>,
934              <code class="computeroutput"><span class="keyword">true</span></code> otherwise.
935            </p></dd>
936<dt><span class="term">Note:</span></dt>
937<dd><p>
938              See <span class="bold"><strong>Note</strong></span> for <a class="link" href="conditions.html#condition_variable_wait"><code class="computeroutput">condition_variable::wait()</code></a>.
939            </p></dd>
940</dl>
941</div>
942<p>
943        </p>
944<h5>
945<a name="condition_variable_wait_for_bridgehead"></a>
946  <span class="phrase"><a name="condition_variable_wait_for"></a></span>
947  <a class="link" href="conditions.html#condition_variable_wait_for">Templated
948        member function <code class="computeroutput">wait_for</code>()</a>
949</h5>
950<p>
951      </p>
952<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span>
953<span class="identifier">cv_status</span> <span class="identifier">wait_for</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span>
954                    <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rel_time</span><span class="special">);</span>
955
956<span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span>
957<span class="keyword">bool</span> <span class="identifier">wait_for</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span>
958               <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rel_time</span><span class="special">,</span>
959               <span class="identifier">Pred</span> <span class="identifier">pred</span><span class="special">);</span>
960</pre>
961<div class="variablelist">
962<p class="title"><b></b></p>
963<dl class="variablelist">
964<dt><span class="term">Precondition:</span></dt>
965<dd><p>
966              <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the
967              current fiber, and either no other fiber is currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>,
968              or the execution of the <code class="computeroutput"><span class="identifier">mutex</span><span class="special">()</span></code> member function on the <code class="computeroutput"><span class="identifier">lk</span></code> objects supplied in the calls
969              to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>
970              in all the fibers currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> would return the same value as
971              <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">mutex</span><span class="special">()</span></code>
972              for this call to <code class="computeroutput"><span class="identifier">wait_for</span></code>.
973            </p></dd>
974<dt><span class="term">Effects:</span></dt>
975<dd>
976<p>
977              Atomically call <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">()</span></code> and blocks the current fiber. The
978              fiber will unblock when notified by a call to <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_all</span><span class="special">()</span></code>, when a time interval equal to or
979              greater than the specified <code class="computeroutput"><span class="identifier">rel_time</span></code>
980              has elapsed. When the fiber is unblocked (for whatever reason), the
981              lock is reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> before the call to <code class="computeroutput"><span class="identifier">wait</span></code> returns. The lock is also reacquired
982              by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code>
983              if the function exits with an exception. The <code class="computeroutput"><span class="identifier">wait_for</span><span class="special">()</span></code> member function accepting <code class="computeroutput"><span class="identifier">pred</span></code> is shorthand for:
984</p>
985<pre class="programlisting"><span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">pred</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span>
986    <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span> <span class="special">==</span> <span class="identifier">wait_for</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">rel_time</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span>
987        <span class="keyword">return</span> <span class="identifier">pred</span><span class="special">();</span>
988    <span class="special">}</span>
989<span class="special">}</span>
990<span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
991</pre>
992<p>
993              (except of course that <code class="computeroutput"><span class="identifier">rel_time</span></code>
994              is adjusted for each iteration). The point is that, even if <code class="computeroutput"><span class="identifier">wait_for</span><span class="special">()</span></code>
995              times out, it can still return <code class="computeroutput"><span class="keyword">true</span></code>
996              if <code class="computeroutput"><span class="identifier">pred</span><span class="special">()</span></code>
997              returns <code class="computeroutput"><span class="keyword">true</span></code> at that time.
998            </p>
999</dd>
1000<dt><span class="term">Postcondition:</span></dt>
1001<dd><p>
1002              <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the
1003              current fiber.
1004            </p></dd>
1005<dt><span class="term">Throws:</span></dt>
1006<dd><p>
1007              <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error
1008              occurs or timeout-related exceptions.
1009            </p></dd>
1010<dt><span class="term">Returns:</span></dt>
1011<dd><p>
1012              The overload without <code class="computeroutput"><span class="identifier">pred</span></code>
1013              returns <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">no_timeout</span></code> if awakened by <code class="computeroutput"><span class="identifier">notify_one</span><span class="special">()</span></code>
1014              or <code class="computeroutput"><span class="identifier">notify_all</span><span class="special">()</span></code>,
1015              or <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span></code> if awakened because at least
1016              <code class="computeroutput"><span class="identifier">rel_time</span></code> has elapsed.
1017            </p></dd>
1018<dt><span class="term">Returns:</span></dt>
1019<dd><p>
1020              The overload accepting <code class="computeroutput"><span class="identifier">pred</span></code>
1021              returns <code class="computeroutput"><span class="keyword">false</span></code> if the call
1022              is returning because at least <code class="computeroutput"><span class="identifier">rel_time</span></code>
1023              has elapsed and the predicate returns <code class="computeroutput"><span class="keyword">false</span></code>,
1024              <code class="computeroutput"><span class="keyword">true</span></code> otherwise.
1025            </p></dd>
1026<dt><span class="term">Note:</span></dt>
1027<dd><p>
1028              See <span class="bold"><strong>Note</strong></span> for <a class="link" href="conditions.html#condition_variable_wait"><code class="computeroutput">condition_variable::wait()</code></a>.
1029            </p></dd>
1030</dl>
1031</div>
1032</div>
1033<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
1034<td align="left"></td>
1035<td align="right"><div class="copyright-footer">Copyright © 2013 Oliver Kowalke<p>
1036        Distributed under the Boost Software License, Version 1.0. (See accompanying
1037        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>)
1038      </p>
1039</div></td>
1040</tr></table>
1041<hr>
1042<div class="spirit-nav">
1043<a accesskey="p" href="mutex_types.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../synchronization.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="barriers.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
1044</div>
1045</body>
1046</html>
1047