1 | <?xml version="1.0" encoding="utf-8"?> |
---|
2 | <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" |
---|
3 | "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd" [ |
---|
4 | <!ENTITY % threads.entities SYSTEM "entities.xml"> |
---|
5 | %threads.entities; |
---|
6 | ]> |
---|
7 | <header name="boost/thread/thread.hpp" |
---|
8 | last-revision="$Date: 2005/12/04 18:11:08 $"> |
---|
9 | <namespace name="boost"> |
---|
10 | <class name="thread"> |
---|
11 | <purpose> |
---|
12 | <para>The <classname>thread</classname> class represents threads of |
---|
13 | execution, and provides the functionality to create and manage |
---|
14 | threads within the &Boost.Threads; library. See |
---|
15 | <xref linkend="threads.glossary"/> for a precise description of |
---|
16 | <link linkend="threads.glossary.thread">thread of execution</link>, |
---|
17 | and for definitions of threading-related terms and of thread states such as |
---|
18 | <link linkend="threads.glossary.thread-state">blocked</link>.</para> |
---|
19 | </purpose> |
---|
20 | |
---|
21 | <description> |
---|
22 | <para>A <link linkend="threads.glossary.thread">thread of execution</link> |
---|
23 | has an initial function. For the program's initial thread, the initial |
---|
24 | function is <code>main()</code>. For other threads, the initial |
---|
25 | function is <code>operator()</code> of the function object passed to |
---|
26 | the <classname>thread</classname> object's constructor.</para> |
---|
27 | |
---|
28 | <para>A thread of execution is said to be "finished" |
---|
29 | or to have "finished execution" when its initial function returns or |
---|
30 | is terminated. This includes completion of all thread cleanup |
---|
31 | handlers, and completion of the normal C++ function return behaviors, |
---|
32 | such as destruction of automatic storage (stack) objects and releasing |
---|
33 | any associated implementation resources.</para> |
---|
34 | |
---|
35 | <para>A thread object has an associated state which is either |
---|
36 | "joinable" or "non-joinable".</para> |
---|
37 | |
---|
38 | <para>Except as described below, the policy used by an implementation |
---|
39 | of &Boost.Threads; to schedule transitions between thread states is |
---|
40 | unspecified.</para> |
---|
41 | |
---|
42 | <para><note>Just as the lifetime of a file may be different from the |
---|
43 | lifetime of an <code>iostream</code> object which represents the file, the lifetime |
---|
44 | of a thread of execution may be different from the |
---|
45 | <classname>thread</classname> object which represents the thread of |
---|
46 | execution. In particular, after a call to <code>join()</code>, |
---|
47 | the thread of execution will no longer exist even though the |
---|
48 | <classname>thread</classname> object continues to exist until the |
---|
49 | end of its normal lifetime. The converse is also possible; if |
---|
50 | a <classname>thread</classname> object is destroyed without |
---|
51 | <code>join()</code> first having been called, the thread of execution |
---|
52 | continues until its initial function completes.</note></para> |
---|
53 | </description> |
---|
54 | |
---|
55 | <inherit access="private"> |
---|
56 | <type><classname>boost::noncopyable</classname></type> |
---|
57 | <purpose>Exposition only</purpose> |
---|
58 | </inherit> |
---|
59 | |
---|
60 | <constructor> |
---|
61 | <effects>Constructs a <classname>thread</classname> object |
---|
62 | representing the current thread of execution.</effects> |
---|
63 | |
---|
64 | <postconditions><code>*this</code> is non-joinable.</postconditions> |
---|
65 | |
---|
66 | <notes><emphasis role="bold">Danger:</emphasis> |
---|
67 | <code>*this</code> is valid only within the current thread.</notes> |
---|
68 | </constructor> |
---|
69 | |
---|
70 | <constructor specifiers="explicit"> |
---|
71 | <parameter name="threadfunc"> |
---|
72 | <paramtype>const boost::function0<void>&</paramtype> |
---|
73 | </parameter> |
---|
74 | |
---|
75 | <effects> |
---|
76 | Starts a new thread of execution and constructs a |
---|
77 | <classname>thread</classname> object representing it. |
---|
78 | Copies <code>threadfunc</code> (which in turn copies |
---|
79 | the function object wrapped by <code>threadfunc</code>) |
---|
80 | to an internal location which persists for the lifetime |
---|
81 | of the new thread of execution. Calls <code>operator()</code> |
---|
82 | on the copy of the <code>threadfunc</code> function object |
---|
83 | in the new thread of execution. |
---|
84 | </effects> |
---|
85 | |
---|
86 | <postconditions><code>*this</code> is joinable.</postconditions> |
---|
87 | |
---|
88 | <throws><code>boost::thread_resource_error</code> if a new thread |
---|
89 | of execution cannot be started.</throws> |
---|
90 | </constructor> |
---|
91 | |
---|
92 | <destructor> |
---|
93 | <effects>Destroys <code>*this</code>. The actual thread of |
---|
94 | execution may continue to execute after the |
---|
95 | <classname>thread</classname> object has been destroyed. |
---|
96 | </effects> |
---|
97 | |
---|
98 | <notes>If <code>*this</code> is joinable the actual thread |
---|
99 | of execution becomes "detached". Any resources used |
---|
100 | by the thread will be reclaimed when the thread of execution |
---|
101 | completes. To ensure such a thread of execution runs to completion |
---|
102 | before the <classname>thread</classname> object is destroyed, call |
---|
103 | <code>join()</code>.</notes> |
---|
104 | </destructor> |
---|
105 | |
---|
106 | <method-group name="comparison"> |
---|
107 | <method name="operator==" cv="const"> |
---|
108 | <type>bool</type> |
---|
109 | |
---|
110 | <parameter name="rhs"> |
---|
111 | <type>const thread&</type> |
---|
112 | </parameter> |
---|
113 | |
---|
114 | <requires>The thread is non-terminated or <code>*this</code> |
---|
115 | is joinable.</requires> |
---|
116 | |
---|
117 | <returns><code>true</code> if <code>*this</code> and |
---|
118 | <code>rhs</code> represent the same thread of |
---|
119 | execution.</returns> |
---|
120 | </method> |
---|
121 | |
---|
122 | <method name="operator!=" cv="const"> |
---|
123 | <type>bool</type> |
---|
124 | |
---|
125 | <parameter name="rhs"> |
---|
126 | <type>const thread&</type> |
---|
127 | </parameter> |
---|
128 | |
---|
129 | <requires>The thread is non-terminated or <code>*this</code> |
---|
130 | is joinable.</requires> |
---|
131 | |
---|
132 | <returns><code>!(*this==rhs)</code>.</returns> |
---|
133 | </method> |
---|
134 | </method-group> |
---|
135 | |
---|
136 | <method-group name="modifier"> |
---|
137 | <method name="join"> |
---|
138 | <type>void</type> |
---|
139 | |
---|
140 | <requires><code>*this</code> is joinable.</requires> |
---|
141 | |
---|
142 | <effects>The current thread of execution blocks until the |
---|
143 | initial function of the thread of execution represented by |
---|
144 | <code>*this</code> finishes and all resources are |
---|
145 | reclaimed.</effects> |
---|
146 | |
---|
147 | <postcondition><code>*this</code> is non-joinable.</postcondition> |
---|
148 | |
---|
149 | <notes>If <code>*this == thread()</code> the result is |
---|
150 | implementation-defined. If the implementation doesn't |
---|
151 | detect this the result will be |
---|
152 | <link linkend="threads.glossary.deadlock">deadlock</link>. |
---|
153 | </notes> |
---|
154 | </method> |
---|
155 | </method-group> |
---|
156 | |
---|
157 | <method-group name="static"> |
---|
158 | <method name="sleep" specifiers="static"> |
---|
159 | <type>void</type> |
---|
160 | |
---|
161 | <parameter name="xt"> |
---|
162 | <paramtype>const <classname>xtime</classname>&</paramtype> |
---|
163 | </parameter> |
---|
164 | |
---|
165 | <effects>The current thread of execution blocks until |
---|
166 | <code>xt</code> is reached.</effects> |
---|
167 | </method> |
---|
168 | |
---|
169 | <method name="yield" specifiers="static"> |
---|
170 | <type>void</type> |
---|
171 | |
---|
172 | <effects>The current thread of execution is placed in the |
---|
173 | <link linkend="threads.glossary.thread-state">ready</link> |
---|
174 | state.</effects> |
---|
175 | |
---|
176 | <notes> |
---|
177 | <simpara>Allow the current thread to give up the rest of its |
---|
178 | time slice (or other scheduling quota) to another thread. |
---|
179 | Particularly useful in non-preemptive implementations.</simpara> |
---|
180 | </notes> |
---|
181 | </method> |
---|
182 | </method-group> |
---|
183 | </class> |
---|
184 | |
---|
185 | <class name="thread_group"> |
---|
186 | <purpose> |
---|
187 | The <classname>thread_group</classname> class provides a container |
---|
188 | for easy grouping of threads to simplify several common thread |
---|
189 | creation and management idioms. |
---|
190 | </purpose> |
---|
191 | |
---|
192 | <inherit access="private"> |
---|
193 | <type><classname>boost::noncopyable</classname></type> |
---|
194 | <purpose>Exposition only</purpose> |
---|
195 | </inherit> |
---|
196 | |
---|
197 | |
---|
198 | <constructor> |
---|
199 | <effects>Constructs an empty <classname>thread_group</classname> |
---|
200 | container.</effects> |
---|
201 | </constructor> |
---|
202 | |
---|
203 | <destructor> |
---|
204 | <effects>Destroys each contained thread object. Destroys <code>*this</code>.</effects> |
---|
205 | |
---|
206 | <notes>Behavior is undefined if another thread references |
---|
207 | <code>*this </code> during the execution of the destructor. |
---|
208 | </notes> |
---|
209 | </destructor> |
---|
210 | |
---|
211 | <method-group name="modifier"> |
---|
212 | <method name="create_thread"> |
---|
213 | <type><classname>thread</classname>*</type> |
---|
214 | |
---|
215 | <parameter name="threadfunc"> |
---|
216 | <paramtype>const boost::function0<void>&</paramtype> |
---|
217 | </parameter> |
---|
218 | |
---|
219 | <effects>Creates a new <classname>thread</classname> object |
---|
220 | that executes <code>threadfunc</code> and adds it to the |
---|
221 | <code>thread_group</code> container object's list of managed |
---|
222 | <classname>thread</classname> objects.</effects> |
---|
223 | |
---|
224 | <returns>Pointer to the newly created |
---|
225 | <classname>thread</classname> object.</returns> |
---|
226 | </method> |
---|
227 | |
---|
228 | <method name="add_thread"> |
---|
229 | <type>void</type> |
---|
230 | |
---|
231 | <parameter name="thrd"> |
---|
232 | <paramtype><classname>thread</classname>*</paramtype> |
---|
233 | </parameter> |
---|
234 | |
---|
235 | <effects>Adds <code>thrd</code> to the |
---|
236 | <classname>thread_group</classname> object's list of managed |
---|
237 | <classname>thread</classname> objects. The <code>thrd</code> |
---|
238 | object must have been allocated via <code>operator new</code> and will |
---|
239 | be deleted when the group is destroyed.</effects> |
---|
240 | </method> |
---|
241 | |
---|
242 | <method name="remove_thread"> |
---|
243 | <type>void</type> |
---|
244 | |
---|
245 | <parameter name="thrd"> |
---|
246 | <paramtype><classname>thread</classname>*</paramtype> |
---|
247 | </parameter> |
---|
248 | |
---|
249 | <effects>Removes <code>thread</code> from <code>*this</code>'s |
---|
250 | list of managed <classname>thread</classname> objects.</effects> |
---|
251 | |
---|
252 | <throws><emphasis role="bold">???</emphasis> if |
---|
253 | <code>thrd</code> is not in <code>*this</code>'s list |
---|
254 | of managed <classname>thread</classname> objects.</throws> |
---|
255 | </method> |
---|
256 | |
---|
257 | <method name="join_all"> |
---|
258 | <type>void</type> |
---|
259 | |
---|
260 | <effects>Calls <code>join()</code> on each of the managed |
---|
261 | <classname>thread</classname> objects.</effects> |
---|
262 | </method> |
---|
263 | </method-group> |
---|
264 | </class> |
---|
265 | </namespace> |
---|
266 | </header> |
---|