1 | <html> |
---|
2 | <head> |
---|
3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
---|
4 | <title>Class condition</title> |
---|
5 | <link rel="stylesheet" href="boostbook.css" type="text/css"> |
---|
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.69.1"> |
---|
7 | <link rel="start" href="index.html" title="The Boost C++ Libraries"> |
---|
8 | <link rel="up" href="threads/reference.html#id2450200" title="Header <boost/thread/condition.hpp>"> |
---|
9 | <link rel="prev" href="barrier.html" title="Class barrier"> |
---|
10 | <link rel="next" href="lock_error.html" title="Class lock_error"> |
---|
11 | </head> |
---|
12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
---|
13 | <table cellpadding="2" width="100%"> |
---|
14 | <td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../boost.png"></td> |
---|
15 | <td align="center"><a href="../../index.htm">Home</a></td> |
---|
16 | <td align="center"><a href="../../libs/libraries.htm">Libraries</a></td> |
---|
17 | <td align="center"><a href="../../people/people.htm">People</a></td> |
---|
18 | <td align="center"><a href="../../more/faq.htm">FAQ</a></td> |
---|
19 | <td align="center"><a href="../../more/index.htm">More</a></td> |
---|
20 | </table> |
---|
21 | <hr> |
---|
22 | <div class="spirit-nav"> |
---|
23 | <a accesskey="p" href="barrier.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="threads/reference.html#id2450200"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="lock_error.html"><img src="images/next.png" alt="Next"></a> |
---|
24 | </div> |
---|
25 | <div class="refentry" lang="en"> |
---|
26 | <a name="condition"></a><div class="titlepage"></div> |
---|
27 | <div class="refnamediv"> |
---|
28 | <h2><span class="refentrytitle">Class condition</span></h2> |
---|
29 | <p>boost::condition — <p>An object of class <a href="condition.html" title="Class condition">condition</a> is a |
---|
30 | synchronization primitive used to cause a thread to wait until a |
---|
31 | particular shared-data condition (or time) is met.</p></p> |
---|
32 | </div> |
---|
33 | <h2 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv-title">Synopsis</h2> |
---|
34 | <div xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv"><pre class="synopsis"> |
---|
35 | <span class="bold"><strong>class</strong></span> condition : <span class="bold"><strong>private</strong></span> boost::noncopyable <span class="emphasis"><em>// Exposition only</em></span> |
---|
36 | { |
---|
37 | <span class="bold"><strong>public</strong></span>: |
---|
38 | <span class="emphasis"><em>// <a href="condition.html#conditionconstruct-copy-destruct">construct/copy/destruct</a></em></span> |
---|
39 | <a href="condition.html#id2531211-bb">condition</a>(); |
---|
40 | <a href="condition.html#id2604889-bb">~condition</a>(); |
---|
41 | |
---|
42 | <span class="emphasis"><em>// <a href="condition.html#id2616531-bb">notification</a></em></span> |
---|
43 | <span class="type"><span class="bold"><strong>void</strong></span></span> <a href="condition.html#id2534198-bb">notify_one</a>(); |
---|
44 | <span class="type"><span class="bold"><strong>void</strong></span></span> <a href="condition.html#id2617876-bb">notify_all</a>(); |
---|
45 | |
---|
46 | <span class="emphasis"><em>// <a href="condition.html#id2616162-bb">waiting</a></em></span> |
---|
47 | <span class="bold"><strong>template</strong></span><<span class="bold"><strong>typename</strong></span> ScopedLock> <span class="type"><span class="bold"><strong>void</strong></span></span> <a href="condition.html#id2575301-bb">wait</a>(ScopedLock&); |
---|
48 | <span class="bold"><strong>template</strong></span><<span class="bold"><strong>typename</strong></span> ScopedLock, <span class="bold"><strong>typename</strong></span> Pred> <span class="type"><span class="bold"><strong>void</strong></span></span> <a href="condition.html#id2450378-bb">wait</a>(ScopedLock&, Pred); |
---|
49 | <span class="bold"><strong>template</strong></span><<span class="bold"><strong>typename</strong></span> ScopedLock> |
---|
50 | <span class="type"><span class="bold"><strong>bool</strong></span></span> <a href="condition.html#id2592886-bb">timed_wait</a>(ScopedLock&, <span class="bold"><strong>const</strong></span> <a href="xtime.html" title="Struct xtime">boost::xtime</a>&); |
---|
51 | <span class="bold"><strong>template</strong></span><<span class="bold"><strong>typename</strong></span> ScopedLock, <span class="bold"><strong>typename</strong></span> Pred> |
---|
52 | <span class="type"><span class="bold"><strong>bool</strong></span></span> <a href="condition.html#id2529517-bb">timed_wait</a>(ScopedLock&, Pred); |
---|
53 | };</pre></div> |
---|
54 | <div class="refsect1" lang="en"> |
---|
55 | <a name="id2779416"></a><h2>Description</h2> |
---|
56 | <p>A <a href="condition.html" title="Class condition">condition</a> object is always used in |
---|
57 | conjunction with a <a href="threads/concepts.html#threads.concepts.mutexes" title="Mutexes">mutex</a> |
---|
58 | object (an object whose type is a model of a <a href="threads/concepts.html#threads.concepts.Mutex" title="Mutex Concept">Mutex</a> or one of its |
---|
59 | refinements). The mutex object must be locked prior to waiting on the |
---|
60 | condition, which is verified by passing a lock object (an object whose |
---|
61 | type is a model of <a href="threads/concepts.html#threads.concepts.Lock" title="Lock Concept">Lock</a> or |
---|
62 | one of its refinements) to the <a href="condition.html" title="Class condition">condition</a> object's |
---|
63 | wait functions. Upon blocking on the <a href="condition.html" title="Class condition">condition</a> |
---|
64 | object, the thread unlocks the mutex object. When the thread returns |
---|
65 | from a call to one of the <a href="condition.html" title="Class condition">condition</a> object's wait |
---|
66 | functions the mutex object is again locked. The tricky unlock/lock |
---|
67 | sequence is performed automatically by the |
---|
68 | <a href="condition.html" title="Class condition">condition</a> object's wait functions.</p> |
---|
69 | <p>The <a href="condition.html" title="Class condition">condition</a> type is often used to |
---|
70 | implement the Monitor Object and other important patterns (see |
---|
71 | [<span class="citation"><a href="threads.html#threads.bib.SchmidtStalRohnertBuschmann">SchmidtStalRohnertBuschmann</a></span>] and [<span class="citation"><a href="threads.html#threads.bib.Hoare74">Hoare74</a></span>]). Monitors are one |
---|
72 | of the most important patterns for creating reliable multithreaded |
---|
73 | programs.</p> |
---|
74 | <p>See <a href="threads.html#threads.glossary" title="Glossary">Glossary</a> for definitions of <a href="threads.html#threads.glossary.thread-state">thread states</a> |
---|
75 | blocked and ready. Note that "waiting" is a synonym for blocked.</p> |
---|
76 | <div class="refsect2" lang="en"> |
---|
77 | <a name="id2779548"></a><h3> |
---|
78 | <a name="conditionconstruct-copy-destruct"></a><code class="computeroutput">condition</code> construct/copy/destruct</h3> |
---|
79 | <div class="orderedlist"><ol type="1"> |
---|
80 | <li> |
---|
81 | <pre class="literallayout"><a name="id2531211-bb"></a>condition();</pre> |
---|
82 | <p><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Effects</span></b>: |
---|
83 | |
---|
84 | Constructs a <a href="condition.html" title="Class condition">condition</a> |
---|
85 | object.</p> |
---|
86 | </li> |
---|
87 | <li> |
---|
88 | <pre class="literallayout"><a name="id2604889-bb"></a>~condition();</pre> |
---|
89 | <p><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Effects</span></b>: |
---|
90 | |
---|
91 | Destroys <code class="computeroutput">*this</code>.</p> |
---|
92 | </li> |
---|
93 | </ol></div> |
---|
94 | </div> |
---|
95 | <div class="refsect2" lang="en"> |
---|
96 | <a name="id2779628"></a><h3> |
---|
97 | <a name="id2616531-bb"></a><code class="computeroutput">condition</code> notification</h3> |
---|
98 | <div class="orderedlist"><ol type="1"> |
---|
99 | <li> |
---|
100 | <pre class="literallayout"><span class="type"><span class="bold"><strong>void</strong></span></span> <a name="id2534198-bb"></a>notify_one();</pre> |
---|
101 | <p><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Effects</span></b>: |
---|
102 | |
---|
103 | If there is a thread waiting on <code class="computeroutput">*this</code>, |
---|
104 | change that thread's state to ready. Otherwise there is no |
---|
105 | effect.<br><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Notes</span></b>: |
---|
106 | |
---|
107 | If more than one thread is waiting on <code class="computeroutput">*this</code>, |
---|
108 | it is unspecified which is made ready. After returning to a ready |
---|
109 | state the notified thread must still acquire the mutex again (which |
---|
110 | occurs within the call to one of the <a href="condition.html" title="Class condition">condition</a> |
---|
111 | object's wait functions.)</p> |
---|
112 | </li> |
---|
113 | <li> |
---|
114 | <pre class="literallayout"><span class="type"><span class="bold"><strong>void</strong></span></span> <a name="id2617876-bb"></a>notify_all();</pre> |
---|
115 | <p><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Effects</span></b>: |
---|
116 | |
---|
117 | Change the state of all threads waiting on |
---|
118 | <code class="computeroutput">*this</code> to ready. If there are no waiting threads, |
---|
119 | <code class="computeroutput">notify_all()</code> has no effect.</p> |
---|
120 | </li> |
---|
121 | </ol></div> |
---|
122 | </div> |
---|
123 | <div class="refsect2" lang="en"> |
---|
124 | <a name="id2779754"></a><h3> |
---|
125 | <a name="id2616162-bb"></a><code class="computeroutput">condition</code> waiting</h3> |
---|
126 | <div class="orderedlist"><ol type="1"> |
---|
127 | <li> |
---|
128 | <pre class="literallayout"><span class="bold"><strong>template</strong></span><<span class="bold"><strong>typename</strong></span> ScopedLock> <span class="type"><span class="bold"><strong>void</strong></span></span> <a name="id2575301-bb"></a>wait(ScopedLock& lock);</pre> |
---|
129 | <p><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Requires</span></b>: |
---|
130 | |
---|
131 | <code class="computeroutput">ScopedLock</code> meets the <a href="threads/concepts.html#threads.concepts.ScopedLock" title="ScopedLock Concept">ScopedLock</a> |
---|
132 | requirements.<br><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Effects</span></b>: |
---|
133 | |
---|
134 | Releases the lock on the <a href="threads/concepts.html#threads.concepts.mutexes" title="Mutexes">mutex object</a> |
---|
135 | associated with <code class="computeroutput">lock</code>, blocks the current thread of execution |
---|
136 | until readied by a call to <code class="computeroutput">this->notify_one()</code> |
---|
137 | or<code class="computeroutput"> this->notify_all()</code>, and then reacquires the |
---|
138 | lock.<br><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Throws</span></b>: |
---|
139 | |
---|
140 | <a href="lock_error.html" title="Class lock_error">lock_error</a> if |
---|
141 | <code class="computeroutput">!lock.locked()</code></p> |
---|
142 | </li> |
---|
143 | <li> |
---|
144 | <pre class="literallayout"><span class="bold"><strong>template</strong></span><<span class="bold"><strong>typename</strong></span> ScopedLock, <span class="bold"><strong>typename</strong></span> Pred> |
---|
145 | <span class="type"><span class="bold"><strong>void</strong></span></span> <a name="id2450378-bb"></a>wait(ScopedLock& lock, Pred pred);</pre> |
---|
146 | <p><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Requires</span></b>: |
---|
147 | |
---|
148 | <code class="computeroutput">ScopedLock</code> meets the <a href="threads/concepts.html#threads.concepts.ScopedLock" title="ScopedLock Concept">ScopedLock</a> |
---|
149 | requirements and the return from <code class="computeroutput">pred()</code> is |
---|
150 | convertible to <code class="computeroutput">bool</code>.<br><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Effects</span></b>: |
---|
151 | |
---|
152 | As if: <code class="computeroutput">while (!pred()) |
---|
153 | wait(lock)</code><br><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Throws</span></b>: |
---|
154 | |
---|
155 | <a href="lock_error.html" title="Class lock_error">lock_error</a> if |
---|
156 | <code class="computeroutput">!lock.locked()</code></p> |
---|
157 | </li> |
---|
158 | <li> |
---|
159 | <pre class="literallayout"><span class="bold"><strong>template</strong></span><<span class="bold"><strong>typename</strong></span> ScopedLock> |
---|
160 | <span class="type"><span class="bold"><strong>bool</strong></span></span> <a name="id2592886-bb"></a>timed_wait(ScopedLock& lock, <span class="bold"><strong>const</strong></span> <a href="xtime.html" title="Struct xtime">boost::xtime</a>& xt);</pre> |
---|
161 | <p><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Requires</span></b>: |
---|
162 | |
---|
163 | <code class="computeroutput">ScopedLock</code> meets the <a href="threads/concepts.html#threads.concepts.ScopedLock" title="ScopedLock Concept">ScopedLock</a> |
---|
164 | requirements.<br><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Effects</span></b>: |
---|
165 | |
---|
166 | Releases the lock on the <a href="threads/concepts.html#threads.concepts.mutexes" title="Mutexes">mutex object</a> |
---|
167 | associated with <code class="computeroutput">lock</code>, blocks the current thread of execution |
---|
168 | until readied by a call to <code class="computeroutput">this->notify_one()</code> |
---|
169 | or<code class="computeroutput"> this->notify_all()</code>, or until time <code class="computeroutput">xt</code> |
---|
170 | is reached, and then reacquires the lock.<br><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Returns</span></b>: |
---|
171 | |
---|
172 | <code class="computeroutput">false</code> if time <code class="computeroutput">xt</code> is reached, |
---|
173 | otherwise <code class="computeroutput">true</code>.<br><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Throws</span></b>: |
---|
174 | |
---|
175 | <a href="lock_error.html" title="Class lock_error">lock_error</a> if |
---|
176 | <code class="computeroutput">!lock.locked()</code></p> |
---|
177 | </li> |
---|
178 | <li> |
---|
179 | <pre class="literallayout"><span class="bold"><strong>template</strong></span><<span class="bold"><strong>typename</strong></span> ScopedLock, <span class="bold"><strong>typename</strong></span> Pred> |
---|
180 | <span class="type"><span class="bold"><strong>bool</strong></span></span> <a name="id2529517-bb"></a>timed_wait(ScopedLock& lock, Pred pred);</pre> |
---|
181 | <p><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Requires</span></b>: |
---|
182 | |
---|
183 | <code class="computeroutput">ScopedLock</code> meets the <a href="threads/concepts.html#threads.concepts.ScopedLock" title="ScopedLock Concept">ScopedLock</a> |
---|
184 | requirements and the return from <code class="computeroutput">pred()</code> is |
---|
185 | convertible to <code class="computeroutput">bool</code>.<br><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Effects</span></b>: |
---|
186 | |
---|
187 | As if: <code class="computeroutput">while (!pred()) { if (!timed_wait(lock, |
---|
188 | xt)) return false; } return true;</code><br><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Returns</span></b>: |
---|
189 | |
---|
190 | <code class="computeroutput">false</code> if <code class="computeroutput">xt</code> is reached, |
---|
191 | otherwise <code class="computeroutput">true</code>.<br><b xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><span class="term">Throws</span></b>: |
---|
192 | |
---|
193 | <a href="lock_error.html" title="Class lock_error">lock_error</a> if |
---|
194 | <code class="computeroutput">!lock.locked()</code></p> |
---|
195 | </li> |
---|
196 | </ol></div> |
---|
197 | </div> |
---|
198 | </div> |
---|
199 | </div> |
---|
200 | <table width="100%"><tr> |
---|
201 | <td align="left"></td> |
---|
202 | <td align="right"><small>Copyright © 2001-2003 William E. Kempf</small></td> |
---|
203 | </tr></table> |
---|
204 | <hr> |
---|
205 | <div class="spirit-nav"> |
---|
206 | <a accesskey="p" href="barrier.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="threads/reference.html#id2450200"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="lock_error.html"><img src="images/next.png" alt="Next"></a> |
---|
207 | </div> |
---|
208 | </body> |
---|
209 | </html> |
---|