Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/doc/html/threads.html @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

File size: 39.6 KB
RevLine 
[12]1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4<title>Chapter 12. Boost.Threads</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="libraries.html" title="Part I. The Boost C++ Libraries">
9<link rel="prev" href="string_algo/credits.html" title="Credits">
10<link rel="next" href="threads/design.html" title="Design">
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="string_algo/credits.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><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="threads/design.html"><img src="images/next.png" alt="Next"></a>
24</div>
25<div class="chapter" lang="en">
26<div class="titlepage"><div>
27<div><h2 class="title">
28<a name="threads"></a>Chapter 12. Boost.Threads</h2></div>
29<div><div class="author"><h3 class="author">
30<span class="firstname">William</span> <span class="othername">E.</span> <span class="surname">Kempf</span>
31</h3></div></div>
32<div><p class="copyright">Copyright © 2001-2003 William E. Kempf</p></div>
33<div><div class="legalnotice">
34<a name="id2774880"></a><p>Permission to use, copy, modify, distribute and sell this
35      software and its documentation for any purpose is hereby granted
36      without fee, provided that the above copyright notice appear in all
37      copies and that both that copyright notice and this permission notice
38      appear in supporting documentation. William E. Kempf makes no
39      representations about the suitability of this software for any purpose.
40      It is provided "as is" without express or implied warranty.</p>
41</div></div>
42</div></div>
43<div class="toc">
44<p><b>Table of Contents</b></p>
45<dl>
46<dt><span class="section"><a href="threads.html#threads.overview">Overview</a></span></dt>
47<dd><dl>
48<dt><span class="section"><a href="threads.html#threads.introduction">Introduction</a></span></dt>
49<dt><span class="section"><a href="threads.html#id2774960">Dangers</a></span></dt>
50<dt><span class="section"><a href="threads.html#id2775138">C++ Standard Library usage in multithreaded programs</a></span></dt>
51<dt><span class="section"><a href="threads.html#id2775288">Common guarantees for all <span class="bold"><strong>Boost.Threads</strong></span> components</a></span></dt>
52</dl></dd>
53<dt><span class="section"><a href="threads/design.html">Design</a></span></dt>
54<dd><dl>
55<dt><span class="section"><a href="threads/design.html#threads.design.goals">Goals</a></span></dt>
56<dt><span class="section"><a href="threads/design.html#id2775596">Iterative Phases</a></span></dt>
57<dt><span class="section"><a href="threads/design.html#id2775636">Phase 1, Synchronization Primitives</a></span></dt>
58<dt><span class="section"><a href="threads/design.html#threads.design.phase2">Phase 2, Thread Management and Thread Specific Storage</a></span></dt>
59<dt><span class="section"><a href="threads/design.html#id2775808">The Next Phase</a></span></dt>
60</dl></dd>
61<dt><span class="section"><a href="threads/concepts.html">Concepts</a></span></dt>
62<dd><dl><dt><span class="section"><a href="threads/concepts.html#threads.concepts.mutexes">Mutexes</a></span></dt></dl></dd>
63<dt><span class="section"><a href="threads/rationale.html">Rationale</a></span></dt>
64<dd><dl>
65<dt><span class="section"><a href="threads/rationale.html#threads.rationale.Boost.Threads">Rationale for the Creation of <span class="bold"><strong>Boost.Threads</strong></span></a></span></dt>
66<dt><span class="section"><a href="threads/rationale.html#threads.rationale.primitives">Rationale for the Low Level Primitives Supported in <span class="bold"><strong>Boost.Threads</strong></span></a></span></dt>
67<dt><span class="section"><a href="threads/rationale.html#threads.rationale.locks">Rationale for the Lock Design</a></span></dt>
68<dt><span class="section"><a href="threads/rationale.html#threads.rationale.non-copyable">Rationale for NonCopyable Thread Type</a></span></dt>
69<dt><span class="section"><a href="threads/rationale.html#threads.rationale.events">Rationale for not providing <span class="emphasis"><em>Event Variables</em></span></a></span></dt>
70</dl></dd>
71<dt><span class="section"><a href="threads/reference.html">Reference</a></span></dt>
72<dd><dl>
73<dt><span class="section"><a href="threads/reference.html#id2616214">Header &lt;boost/thread/barrier.hpp&gt;</a></span></dt>
74<dt><span class="section"><a href="threads/reference.html#id2450200">Header &lt;boost/thread/condition.hpp&gt;</a></span></dt>
75<dt><span class="section"><a href="threads/reference.html#id2533190">Header &lt;boost/thread/exceptions.hpp&gt;</a></span></dt>
76<dt><span class="section"><a href="threads/reference.html#id2534429">Header &lt;boost/thread/mutex.hpp&gt;</a></span></dt>
77<dt><span class="section"><a href="threads/reference.html#id2534986">Header &lt;boost/thread/once.hpp&gt;</a></span></dt>
78<dt><span class="section"><a href="threads/reference.html#id2436754">Header &lt;boost/thread/recursive_mutex.hpp&gt;</a></span></dt>
79<dt><span class="section"><a href="threads/reference.html#id2617127">Header &lt;boost/thread/thread.hpp&gt;</a></span></dt>
80<dt><span class="section"><a href="threads/reference.html#id2547002">Header &lt;boost/thread/tss.hpp&gt;</a></span></dt>
81<dt><span class="section"><a href="threads/reference.html#id2416096">Header &lt;boost/thread/xtime.hpp&gt;</a></span></dt>
82</dl></dd>
83<dt><span class="section"><a href="threads/faq.html">Frequently Asked Questions</a></span></dt>
84<dt><span class="section"><a href="thread/configuration.html">Configuration</a></span></dt>
85<dd><dl>
86<dt><span class="section"><a href="thread/configuration.html#thread.configuration.public">Library Defined Public Macros</a></span></dt>
87<dt><span class="section"><a href="thread/configuration.html#thread.configuration.implementation">Library Defined Implementation Macros</a></span></dt>
88</dl></dd>
89<dt><span class="section"><a href="thread/build.html">Build</a></span></dt>
90<dd><dl>
91<dt><span class="section"><a href="thread/build.html#thread.build.building">Building the <span class="bold"><strong>Boost.Threads</strong></span> Libraries</a></span></dt>
92<dt><span class="section"><a href="thread/build.html#thread.build.testing">Testing the <span class="bold"><strong>Boost.Threads</strong></span> Libraries</a></span></dt>
93</dl></dd>
94<dt><span class="section"><a href="threads/implementation_notes.html">Implementation Notes</a></span></dt>
95<dd><dl><dt><span class="section"><a href="threads/implementation_notes.html#threads.implementation_notes.win32">Win32</a></span></dt></dl></dd>
96<dt><span class="section"><a href="threads/release_notes.html">Release Notes</a></span></dt>
97<dd><dl><dt><span class="section"><a href="threads/release_notes.html#threads.release_notes.boost_1_32_0">Boost 1.32.0</a></span></dt></dl></dd>
98<dt><span class="glossary"><a href="threads.html#threads.glossary">Glossary</a></span></dt>
99<dt><span class="section"><a href="thread/acknowledgements.html">Acknowledgements</a></span></dt>
100<dt><span class="bibliography"><a href="threads.html#threads.bibliography">Bibliography</a></span></dt>
101</dl>
102</div>
103<div class="section" lang="en">
104<div class="titlepage"><div><div><h3 class="title">
105<a name="threads.overview"></a>Overview</h3></div></div></div>
106<div class="toc"><dl>
107<dt><span class="section"><a href="threads.html#threads.introduction">Introduction</a></span></dt>
108<dt><span class="section"><a href="threads.html#id2774960">Dangers</a></span></dt>
109<dt><span class="section"><a href="threads.html#id2775138">C++ Standard Library usage in multithreaded programs</a></span></dt>
110<dt><span class="section"><a href="threads.html#id2775288">Common guarantees for all <span class="bold"><strong>Boost.Threads</strong></span> components</a></span></dt>
111</dl></div>
112<div class="section" lang="en">
113<div class="titlepage"><div><div><h4 class="title">
114<a name="threads.introduction"></a>Introduction</h4></div></div></div>
115<p><span class="bold"><strong>Boost.Threads</strong></span> allows C++ programs to execute as multiple,
116    asynchronous, independent threads-of-execution. Each thread has its own
117    machine state including program instruction counter and registers. Programs
118    which execute as multiple threads are called multithreaded programs to
119    distinguish them from traditional single-threaded programs. The <a href="threads.html#threads.glossary" title="Glossary">glossary</a> gives a more complete description
120        of the multithreading execution environment.</p>
121<p>Multithreading provides several advantages:
122    </p>
123<div class="itemizedlist"><ul type="disc">
124<li><p>Programs which would otherwise block waiting for some external
125        event can continue to respond if the blocking operation is placed in a
126        separate thread. Multithreading is usually an absolute requirement for
127        these programs.</p></li>
128<li><p>Well-designed multithreaded programs may execute faster than
129        single-threaded programs, particularly on multiprocessor hardware.
130        Note, however, that poorly-designed multithreaded programs are often
131        slower than single-threaded programs.</p></li>
132<li><p>Some program designs may be easier to formulate using a
133        multithreaded approach. After all, the real world is
134        asynchronous!</p></li>
135</ul></div>
136</div>
137<div class="section" lang="en">
138<div class="titlepage"><div><div><h4 class="title">
139<a name="id2774960"></a>Dangers</h4></div></div></div>
140<div class="toc"><dl>
141<dt><span class="section"><a href="threads.html#id2774964">General considerations</a></span></dt>
142<dt><span class="section"><a href="threads.html#id2775042">Testing and debugging considerations</a></span></dt>
143<dt><span class="section"><a href="threads.html#id2775078">Getting a head start</a></span></dt>
144</dl></div>
145<div class="section" lang="en">
146<div class="titlepage"><div><div><h5 class="title">
147<a name="id2774964"></a>General considerations</h5></div></div></div>
148<p>Beyond the errors which can occur in single-threaded programs,
149    multithreaded programs are subject to additional errors:
150    </p>
151<div class="itemizedlist"><ul type="disc">
152<li><p><a href="threads.html#threads.glossary.race-condition">Race
153            conditions</a></p></li>
154<li><p><a href="threads.html#threads.glossary.deadlock">Deadlock</a>
155        (sometimes called "deadly embrace")</p></li>
156<li><p><a href="threads.html#threads.glossary.priority-failure">Priority
157        failures</a> (priority inversion, infinite overtaking, starvation,
158                etc.)</p></li>
159</ul></div>
160<p>Every multithreaded program must be designed carefully to avoid these
161        errors. These aren't rare or exotic failures - they are virtually guaranteed
162        to occur unless multithreaded code is designed to avoid them. Priority
163        failures are somewhat less common, but are nonetheless serious.</p>
164<p>The <a href="threads/design.html" title="Design"><span class="bold"><strong>Boost.Threads</strong></span> design</a>
165    attempts to minimize these errors, but they will still occur unless the
166    programmer proactively designs to avoid them.</p>
167<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
168<h3 class="title">Note</h3>Please also see <a href="threads/implementation_notes.html" title="Implementation Notes">the section called &#8220;Implementation Notes&#8221;</a>
169        for additional, implementation-specific considerations.</div>
170</div>
171<div class="section" lang="en">
172<div class="titlepage"><div><div><h5 class="title">
173<a name="id2775042"></a>Testing and debugging considerations</h5></div></div></div>
174<p>Multithreaded programs are non-deterministic. In other words, the
175      same program with the same input data may follow different execution
176      paths each time it is invoked. That can make testing and debugging a
177      nightmare:
178      </p>
179<div class="itemizedlist"><ul type="disc">
180<li><p>Failures are often not repeatable.</p></li>
181<li><p>Probe effect causes debuggers to produce very different results
182          from non-debug uses.</p></li>
183<li><p>Debuggers require special support to show thread state.</p></li>
184<li><p>Tests on a single processor system may give no indication of
185          serious errors which would appear on multiprocessor systems, and visa
186          versa. Thus test cases should include a varying number of
187          processors.</p></li>
188<li><p>For programs which create a varying number of threads according
189          to workload, tests which don't span the full range of possibilities
190          may miss serious errors.</p></li>
191</ul></div>
192</div>
193<div class="section" lang="en">
194<div class="titlepage"><div><div><h5 class="title">
195<a name="id2775078"></a>Getting a head start</h5></div></div></div>
196<p>Although it might appear that multithreaded programs are inherently
197      unreliable, many reliable multithreaded programs do exist. Multithreading
198      techniques are known which lead to reliable programs.</p>
199<p>Design patterns for reliable multithreaded programs, including the
200      important <span class="emphasis"><em>monitor</em></span> pattern, are presented in
201      <span class="emphasis"><em>Pattern-Oriented Software Architecture Volume 2 - Patterns for
202      Concurrent and Networked Objects</em></span>[<span class="citation"><a href="threads.html#threads.bib.SchmidtStalRohnertBuschmann">SchmidtStalRohnertBuschmann</a></span>]. Many important multithreading programming
203          considerations (independent of threading library) are discussed in
204          <span class="emphasis"><em>Programming with POSIX Threads</em></span>[<span class="citation"><a href="threads.html#threads.bib.Butenhof97">Butenhof97</a></span>].</p>
205<p>Doing some reading before attempting multithreaded designs will
206      give you a head start toward reliable multithreaded programs.</p>
207</div>
208</div>
209<div class="section" lang="en">
210<div class="titlepage"><div><div><h4 class="title">
211<a name="id2775138"></a>C++ Standard Library usage in multithreaded programs</h4></div></div></div>
212<div class="toc"><dl>
213<dt><span class="section"><a href="threads.html#id2775143">Runtime libraries</a></span></dt>
214<dt><span class="section"><a href="threads.html#id2775193">Potentially non-thread-safe functions</a></span></dt>
215</dl></div>
216<div class="section" lang="en">
217<div class="titlepage"><div><div><h5 class="title">
218<a name="id2775143"></a>Runtime libraries</h5></div></div></div>
219<p><span class="bold"><strong>Warning:</strong></span> Multithreaded programs such as
220          those using <span class="bold"><strong>Boost.Threads</strong></span> must link to <a href="threads.html#threads.glossary.thread-safe">thread-safe</a> versions of
221          all runtime libraries used by the program, including the runtime library
222          for the C++ Standard Library. Failure to do so will cause <a href="threads.html#threads.glossary.race-condition">race conditions</a> to occur
223          when multiple threads simultaneously execute runtime library functions for
224          <code class="computeroutput">new</code>, <code class="computeroutput">delete</code>, or other language features which
225          imply shared state.</p>
226</div>
227<div class="section" lang="en">
228<div class="titlepage"><div><div><h5 class="title">
229<a name="id2775193"></a>Potentially non-thread-safe functions</h5></div></div></div>
230<p>Certain C++ Standard Library functions inherited from C are
231      particular problems because they hold internal state between
232      calls:
233      </p>
234<div class="itemizedlist"><ul type="disc">
235<li><p><code class="computeroutput">rand</code></p></li>
236<li><p><code class="computeroutput">strtok</code></p></li>
237<li><p><code class="computeroutput">asctime</code></p></li>
238<li><p><code class="computeroutput">ctime</code></p></li>
239<li><p><code class="computeroutput">gmtime</code></p></li>
240<li><p><code class="computeroutput">localtime</code></p></li>
241</ul></div>
242<p>It is possible to write thread-safe implementations of these by
243      using thread specific storage (see
244          <code class="computeroutput"><a href="thread_specific_ptr.html" title="Class thread_specific_ptr">boost::thread_specific_ptr</a></code>), and several C++
245          compiler vendors do just that. The technique is well-know and is explained
246          in [<span class="citation"><a href="threads.html#threads.bib.Butenhof97">Butenhof97</a></span>].</p>
247<p>But at least one vendor (HP-UX) does not provide thread-safe
248      implementations of the above functions in their otherwise thread-safe
249      runtime library. Instead they provide replacement functions with
250      different names and arguments.</p>
251<p><span class="bold"><strong>Recommendation:</strong></span> For the most
252          portable, yet thread-safe code, use Boost replacements for the problem
253          functions. See the Boost Random Number Library
254          and Boost Tokenizer Library.</p>
255</div>
256</div>
257<div class="section" lang="en">
258<div class="titlepage"><div><div><h4 class="title">
259<a name="id2775288"></a>Common guarantees for all <span class="bold"><strong>Boost.Threads</strong></span> components</h4></div></div></div>
260<div class="toc"><dl>
261<dt><span class="section"><a href="threads.html#id2775298">Exceptions</a></span></dt>
262<dt><span class="section"><a href="threads.html#id2775375">NonCopyable requirement</a></span></dt>
263</dl></div>
264<div class="section" lang="en">
265<div class="titlepage"><div><div><h5 class="title">
266<a name="id2775298"></a>Exceptions</h5></div></div></div>
267<p><span class="bold"><strong>Boost.Threads</strong></span> destructors never
268          throw exceptions. Unless otherwise specified, other
269          <span class="bold"><strong>Boost.Threads</strong></span> functions that do not have
270          an exception-specification may throw implementation-defined
271          exceptions.</p>
272<p>In particular, <span class="bold"><strong>Boost.Threads</strong></span>
273          reports failure to allocate storage by throwing an exception of type
274          <code class="computeroutput">std::bad_alloc</code> or a class derived from
275          <code class="computeroutput">std::bad_alloc</code>, failure to obtain thread resources other than
276          memory by throwing an exception of type
277          <code class="computeroutput"><a href="thread_resource_error.html" title="Class thread_resource_error">boost::thread_resource_error</a></code>, and certain lock
278          related failures by throwing an exception of type
279          <code class="computeroutput"><a href="lock_error.html" title="Class lock_error">boost::lock_error</a></code>.</p>
280<p><span class="bold"><strong>Rationale:</strong></span> Follows the C++ Standard
281          Library practice of allowing all functions except destructors or other
282          specified functions to throw exceptions on errors.</p>
283</div>
284<div class="section" lang="en">
285<div class="titlepage"><div><div><h5 class="title">
286<a name="id2775375"></a>NonCopyable requirement</h5></div></div></div>
287<p><span class="bold"><strong>Boost.Threads</strong></span> classes documented as
288          meeting the NonCopyable requirement disallow copy construction and copy
289          assignment. For the sake of exposition, the synopsis of such classes show
290          private derivation from <code class="computeroutput">boost::noncopyable</code>. Users
291          should not depend on this derivation, however, as implementations are free
292          to meet the NonCopyable requirement in other ways.</p>
293</div>
294</div>
295</div>
296<div class="glossary">
297<div class="titlepage"><div><div><h2 class="title">
298<a name="threads.glossary"></a>Glossary</h2></div></div></div>
299<p>Definitions are given in terms of the C++ Standard
300  [<span class="citation"><a href="threads.html#threads.bib.ISO98">ISO98</a></span>]. References to the standard are in the form [1.2.3/4], which
301  represents the section number, with the paragraph number following the
302  "/".</p>
303<p>Because the definitions are written in something akin to "standardese",
304  they can be difficult to understand. The intent isn't to confuse, but rather
305  to clarify the additional requirements <span class="bold"><strong>Boost.Threads</strong></span> places on a C++
306  implementation as defined by the C++ Standard.</p>
307<dl>
308<dt>
309<a name="threads.glossary.thread"></a>Thread</dt>
310<dd>
311<p>Thread is short for "thread of execution". A thread of execution is
312          an execution environment [1.9/7] within the execution environment of a C++
313          program [1.9]. The main() function [3.6.1] of the program is the initial
314          function of the initial thread. A program in a multithreading environment
315          always has an initial thread even if the program explicitly creates no
316          additional threads.</p>
317<p>Unless otherwise specified, each thread shares all aspects of its
318          execution environment with other threads in the program. Shared aspects of
319          the execution environment include, but are not limited to, the
320          following:</p>
321<div class="itemizedlist"><ul type="disc">
322<li><p>Static storage duration (static, extern) objects
323                [3.7.1].</p></li>
324<li><p>Dynamic storage duration (heap) objects [3.7.3]. Thus
325                each memory allocation will return a unique addresses, regardless of the
326                thread making the allocation request.</p></li>
327<li><p>Automatic storage duration (stack) objects [3.7.2]
328                accessed via pointer or reference from another thread.</p></li>
329<li><p>Resources provided by the operating system. For example,
330                files.</p></li>
331<li><p>The program itself. In other words, each thread is
332                executing some function of the same program, not a totally different
333                program.</p></li>
334</ul></div>
335<p>Each thread has its own:</p>
336<div class="itemizedlist"><ul type="disc">
337<li><p>Registers and current execution sequence (program
338                counter) [1.9/5].</p></li>
339<li><p>Automatic storage duration (stack) objects
340                [3.7.2].</p></li>
341</ul></div>
342</dd>
343<dt>
344<a name="threads.glossary.thread-safe"></a>Thread-safe</dt>
345<dd>
346<p>A program is thread-safe if it has no <a href="threads.html#threads.glossary.race-condition">race conditions</a>, does
347          not <a href="threads.html#threads.glossary.deadlock">deadlock</a>, and has
348          no <a href="threads.html#threads.glossary.priority-failure">priority
349          failures</a>.</p>
350<p>Note that thread-safety does not necessarily imply efficiency, and
351          than while some thread-safety violations can be determined statically at
352          compile time, many thread-safety errors can only only be detected at
353          runtime.</p>
354</dd>
355<dt>
356<a name="threads.glossary.thread-state"></a>Thread State</dt>
357<dd>
358<p>During the lifetime of a thread, it shall be in one of the following
359          states:</p>
360<div class="table">
361<a name="id2787953"></a><p class="title"><b>Table 12.15. Thread States</b></p>
362<table class="table" summary="Thread States">
363<colgroup>
364<col>
365<col>
366</colgroup>
367<thead><tr>
368<th align="left">State</th>
369<th align="left">Description</th>
370</tr></thead>
371<tbody>
372<tr>
373<td align="left">Ready</td>
374<td align="left">Ready to run, but waiting for a processor.</td>
375</tr>
376<tr>
377<td align="left">Running</td>
378<td align="left">Currently executing on a processor. Zero or more threads
379                                        may be running at any time, with a maximum equal to the number of
380                                        processors.</td>
381</tr>
382<tr>
383<td align="left">Blocked</td>
384<td align="left">Waiting for some resource other than a processor which is
385                                        not currently available, or for the completion of calls to library
386                                        functions [1.9/6]. The term "waiting" is synonymous with
387                                        "blocked"</td>
388</tr>
389<tr>
390<td align="left">Terminated</td>
391<td align="left">Finished execution but not yet detached or joined.</td>
392</tr>
393</tbody>
394</table>
395</div>
396<p>Thread state transitions shall occur only as specified:</p>
397<div class="table">
398<a name="id2788003"></a><p class="title"><b>Table 12.16. Thread States Transitions</b></p>
399<table class="table" summary="Thread States Transitions">
400<colgroup>
401<col>
402<col>
403<col>
404</colgroup>
405<thead><tr>
406<th align="left">From</th>
407<th align="left">To</th>
408<th align="left">Cause</th>
409</tr></thead>
410<tbody>
411<tr>
412<td align="left">[none]</td>
413<td align="left">Ready</td>
414<td align="left"><p>Thread is created by a call to a library function.
415                                        In the case of the initial thread, creation is implicit and
416                                        occurs during the startup of the main() function [3.6.1].</p></td>
417</tr>
418<tr>
419<td align="left">Ready</td>
420<td align="left">Running</td>
421<td align="left"><p>Processor becomes available.</p></td>
422</tr>
423<tr>
424<td align="left">Running</td>
425<td align="left">Ready</td>
426<td align="left">Thread preempted.</td>
427</tr>
428<tr>
429<td align="left">Running</td>
430<td align="left">Blocked</td>
431<td align="left">Thread calls a library function which waits for a resource or
432                                        for the completion of I/O.</td>
433</tr>
434<tr>
435<td align="left">Running</td>
436<td align="left">Terminated</td>
437<td align="left">Thread returns from its initial function, calls a thread
438                                        termination library function, or is canceled by some other thread
439                                        calling a thread termination library function.</td>
440</tr>
441<tr>
442<td align="left">Blocked</td>
443<td align="left">Ready</td>
444<td align="left">The resource being waited for becomes available, or the
445                                        blocking library function completes.</td>
446</tr>
447<tr>
448<td align="left">Terminated</td>
449<td align="left">[none]</td>
450<td align="left">Thread is detached or joined by some other thread calling the
451                                        appropriate library function, or by program termination
452                                        [3.6.3].</td>
453</tr>
454</tbody>
455</table>
456</div>
457<p>[Note: if a suspend() function is added to the threading library,
458          additional transitions to the blocked state will have to be added to the
459          above table.]</p>
460</dd>
461<dt>
462<a name="threads.glossary.race-condition"></a>Race Condition</dt>
463<dd>
464<p>A race condition is what occurs when multiple threads read from and write
465          to the same memory without proper synchronization, resulting in an incorrect
466          value being read or written. The result of a race condition may be a bit
467          pattern which isn't even a valid value for the data type. A race condition
468          results in undefined behavior [1.3.12].</p>
469<p>Race conditions can be prevented by serializing memory access using
470          the tools provided by <span class="bold"><strong>Boost.Threads</strong></span>.</p>
471</dd>
472<dt>
473<a name="threads.glossary.deadlock"></a>Deadlock</dt>
474<dd><p>Deadlock is an execution state where for some set of threads, each
475          thread in the set is blocked waiting for some action by one of the other
476          threads in the set. Since each is waiting on the others, none will ever
477          become ready again.</p></dd>
478<dt>
479<a name="threads.glossary.starvation"></a>Starvation</dt>
480<dd><p>The condition in which a thread is not making sufficient progress in
481          its work during a given time interval.</p></dd>
482<dt>
483<a name="threads.glossary.priority-failure"></a>Priority Failure</dt>
484<dd><p>A priority failure (such as priority inversion or infinite overtaking)
485          occurs when threads are executed in such a sequence that required work is not
486          performed in time to be useful.</p></dd>
487<dt>
488<a name="threads.glossary.undefined-behavior"></a>Undefined Behavior</dt>
489<dd>
490<p>The result of certain operations in <span class="bold"><strong>Boost.Threads</strong></span> is undefined;
491          this means that those operations can invoke almost any behavior when
492          they are executed.</p>
493<p>An operation whose behavior is undefined can work "correctly"
494          in some implementations (i.e., do what the programmer thought it
495          would do), while in other implementations it may exhibit almost
496          any "incorrect" behavior--such as returning an invalid value,
497          throwing an exception, generating an access violation, or terminating
498          the process.</p>
499<p>Executing a statement whose behavior is undefined is a
500          programming error.</p>
501</dd>
502<dt>
503<a name="threads.glossary.memory-visibility"></a>Memory Visibility</dt>
504<dd>
505<p>An address [1.7] shall always point to the same memory byte,
506          regardless of the thread or processor dereferencing the address.</p>
507<p>An object [1.8, 1.9] is accessible from multiple threads if it is of
508          static storage duration (static, extern) [3.7.1], or if a pointer or
509          reference to it is explicitly or implicitly dereferenced in multiple
510          threads.</p>
511<p>For an object accessible from multiple threads, the value of the
512          object accessed from one thread may be indeterminate or different from the
513          value accessed from another thread, except under the conditions specified in
514          the following table. For the same row of the table, the value of an object
515          accessible at the indicated sequence point in thread A will be determinate
516          and the same if accessed at or after the indicated sequence point in thread
517          B, provided the object is not otherwise modified. In the table, the
518          "sequence point at a call" is the sequence point after the evaluation of all
519          function arguments [1.9/17], while the "sequence point after a call" is the
520          sequence point after the copying of the returned value... [1.9/17].</p>
521<div class="table">
522<a name="id2788233"></a><p class="title"><b>Table 12.17. Memory Visibility</b></p>
523<table class="table" summary="Memory Visibility">
524<colgroup>
525<col>
526<col>
527</colgroup>
528<thead><tr>
529<th>Thread A</th>
530<th>Thread B</th>
531</tr></thead>
532<tbody>
533<tr>
534<td>The sequence point at a call to a library thread-creation
535                                        function.</td>
536<td>The first sequence point of the initial function in the new
537                                        thread created by the Thread A call.</td>
538</tr>
539<tr>
540<td>The sequence point at a call to a library function which
541                                        locks a mutex, directly or by waiting for a condition
542                                        variable.</td>
543<td>The sequence point after a call to a library function which
544                                        unlocks the same mutex.</td>
545</tr>
546<tr>
547<td>The last sequence point before thread termination.</td>
548<td>The sequence point after a call to a library function which
549                                        joins the terminated thread.</td>
550</tr>
551<tr>
552<td>The sequence point at a call to a library function which
553                                        signals or broadcasts a condition variable.</td>
554<td>The sequence point after the call to the library function
555                                        which was waiting on that same condition variable or signal.</td>
556</tr>
557</tbody>
558</table>
559</div>
560<p>The architecture of the execution environment and the observable
561          behavior of the abstract machine [1.9] shall be the same on all
562          processors.</p>
563<p>The latitude granted by the C++ standard for an implementation to
564          alter the definition of observable behavior of the abstract machine to
565          include additional library I/O functions [1.9/6] is extended to include
566          threading library functions.</p>
567<p>When an exception is thrown and there is no matching exception handler
568          in the same thread, behavior is undefined. The preferred behavior is the
569          same as when there is no matching exception handler in a program
570          [15.3/9]. That is, terminate() is called, and it is implementation-defined
571          whether or not the stack is unwound.</p>
572</dd>
573</dl>
574</div>
575<div class="bibliography">
576<div class="titlepage"><div><div><h2 class="title">
577<a name="threads.bibliography"></a>Bibliography</h2></div></div></div>
578<div class="biblioentry">
579<a name="threads.bib.AndrewsSchneider83"></a><p>[<span class="abbrev"><a name="threads.bib.AndrewsSchneider83.abbrev"></a>AndrewsSchnieder83</span>] <span class="biblioset"><i>ACM Computing Surveys</i>. <span class="volumenum">Vol. 15. </span><span class="issuenum">No. 1. </span><span class="date">March, 1983. </span></span><span class="biblioset"><span class="authorgroup"><span class="firstname">Gregory</span> <span class="othername">R.</span> <span class="surname">Andrews</span> and <span class="firstname">Fred</span> <span class="othername">B.</span> <span class="surname">Schneider</span>. </span>&#8220;<a href="http://www.acm.org/pubs/citations/journals/surveys/1983-15-1/p3-andrews/" target="_top">Concepts and Notations for Concurrent Programming</a>&#8221;. </span><p>Good general background reading. Includes descriptions of Path
580    Expressions, Message Passing, and Remote Procedure Call in addition to the
581    basics</p></p>
582</div>
583<div class="biblioentry">
584<a name="threads.bib.Boost"></a><p>[<span class="abbrev"><a name="threads.bib.Boost.abbrev"></a>Boost</span>] <span class="bibliomisc">The <span class="emphasis"><em>Boost</em></span> world wide web site.
585        <a href="http:/www.boost.org" target="_top">http://www.boost.org</a>. </span><p><span class="bold"><strong>Boost.Threads</strong></span> is one of many Boost libraries. The Boost web
586    site includes a great deal of documentation and general information which
587    applies to all Boost libraries. Current copies of the libraries including
588    documentation and test programs may be downloaded from the web
589    site.</p></p>
590</div>
591<div class="biblioentry">
592<a name="threads.bib.Hansen73"></a><p>[<span class="abbrev"><a name="threads.bib.Hansen73.abbrev"></a>Hansen73</span>] <span class="biblioset"><i>ACM Computing Surveys</i>. <span class="volumenum">Vol. 5. </span><span class="issuenum">No. 4. </span><span class="date">December, 1973. </span></span><span class="biblioset"><span class="author"><span class="firstname">Per Brinch</span>. </span>&#8220;<a href="http://www.acm.org/pubs/articles/journals/surveys/1973-5-4/p223-hansen/" target="_top">Concurrent Programming Concepts</a>&#8221;. </span><p>"This paper describes the evolution of language features for
593    multiprogramming from event queues and semaphores to critical regions and
594    monitors." Includes analysis of why events are considered error-prone. Also
595    noteworthy because of an introductory quotation from Christopher Alexander;
596    Brinch Hansen was years ahead of others in recognizing pattern concepts
597    applied to software, too.</p></p>
598</div>
599<div class="biblioentry">
600<a name="threads.bib.Butenhof97"></a><p>[<span class="abbrev"><a name="threads.bib.Butenhof97.abbrev"></a>Butenhof97</span>] <span class="title"><i><a href="http://cseng.aw.com/book/0,3828,0201633922,00.html" target="_top">Programming with POSIX Threads </a></i>. </span><span class="author"><span class="firstname">David</span> <span class="othername">R.</span> <span class="surname">Butenhof</span>. </span><span class="publisher">Addison-Wesley</span><span class="copyright">Copyright © 1997. </span><span class="isbn">ISNB: 0-201-63392-2. </span><p>This is a very readable explanation of threads and how to use
601        them. Many of the insights given apply to all multithreaded programming, not
602        just POSIX Threads</p></p>
603</div>
604<div class="biblioentry">
605<a name="threads.bib.Hoare74"></a><p>[<span class="abbrev"><a name="threads.bib.Hoare74.abbrev"></a>Hoare74</span>] <span class="biblioset"><i>Communications of the ACM</i>. <span class="volumenum">Vol. 17. </span><span class="issuenum">No. 10. </span><span class="date">October, 1974. </span></span><span class="biblioset">&#8220;<a href="http://www.acm.org/classics/feb96/" target="_top">Monitors: An Operating System Structuring Concept</a>&#8221;. <span class="author"><span class="firstname">C.A.R.</span> <span class="surname">Hoare</span>. </span><span class="pagenums">549-557. </span></span><p>Hoare and Brinch Hansen's work on Monitors is the basis for reliable
606        multithreading patterns. This is one of the most often referenced papers in
607        all of computer science, and with good reason.</p></p>
608</div>
609<div class="biblioentry">
610<a name="threads.bib.ISO98"></a><p>[<span class="abbrev"><a name="threads.bib.ISO98.abbrev"></a>ISO98</span>] <span class="title"><i><a href="http://www.ansi.org" target="_top">Programming Language C++</a></i>. </span><span class="orgname">ISO/IEC. </span><span class="releaseinfo">14882:1998(E). </span><p>This is the official C++ Standards document. Available from the ANSI
611        (American National Standards Institute) Electronic Standards Store.</p></p>
612</div>
613<div class="biblioentry">
614<a name="threads.bib.McDowellHelmbold89"></a><p>[<span class="abbrev"><a name="threads.bib.McDowellHelmbold89.abbrev"></a>McDowellHelmbold89</span>] <span class="biblioset"><i>Communications of the ACM</i>. <span class="volumenum">Vol. 21. </span><span class="issuenum">No. 2. </span><span class="date">December, 1989. </span></span><span class="biblioset"><span class="author"><span class="firstname">Charles</span> <span class="othername">E.</span> <span class="surname">McDowell</span>. </span><span class="author"><span class="firstname">David</span> <span class="othername">P.</span> <span class="surname">Helmbold</span>. </span><i><a href="http://www.acm.org/pubs/citations/journals/surveys/1989-21-4/p593-mcdowell/" target="_top">Debugging Concurrent Programs</a></i>. </span><p>Identifies many of the unique failure modes and debugging difficulties
615        associated with concurrent programs.</p></p>
616</div>
617<div class="biblioentry">
618<a name="threads.bib.SchmidtPyarali"></a><p>[<span class="abbrev"><a name="threads.bib.SchmidtPyarali.abbrev"></a>SchmidtPyarali</span>] <span class="title"><i><a href="http://www.cs.wustl.edu/~schmidt/win32-cv-1.html8" target="_top">Strategies for Implementing POSIX Condition Variables on Win32</a></i>. </span><span class="authorgroup"><span class="firstname">Douglas</span> <span class="othername">C.</span> <span class="surname">Schmidt</span> and <span class="firstname">Irfan</span> <span class="surname">Pyarali</span>. </span><span class="orgname">Department of Computer Science, Washington University, St. Louis,
619        Missouri. </span><p>Rationale for understanding <span class="bold"><strong>Boost.Threads</strong></span> condition
620        variables. Note that Alexander Terekhov found some bugs in the
621        implementation given in this article, so pthreads-win32 and <span class="bold"><strong>Boost.Threads</strong></span>
622        are even more complicated yet.</p></p>
623</div>
624<div class="biblioentry">
625<a name="threads.bib.SchmidtStalRohnertBuschmann"></a><p>[<span class="abbrev"><a name="threads.bib.SchmidtStalRohnertBuschmann.abbrev"></a>SchmidtStalRohnertBuschmann</span>] <span class="title"><i><a href="http://www.wiley.com/Corporate/Website/Objects/Products/0,9049,104671,00.html" target="_top">Pattern-Oriented Architecture Volume 2</a></i>. </span><span class="subtitle">Patterns for Concurrent and Networked Objects. </span><span class="titleabbrev">POSA2. </span><span class="authorgroup"><span class="firstname">Douglas</span> <span class="othername">C.</span> <span class="surname">Schmidt</span>, <span class="firstname">Michael</span>, <span class="firstname">Hans</span> <span class="surname">Rohnert</span>, and <span class="firstname">Frank</span> <span class="surname">Buschmann</span>. </span><span class="publisher">Wiley</span><span class="copyright">Copyright © 2000. </span><p>This is a very good explanation of how to apply several patterns
626        useful for concurrent programming. Among the patterns documented is the
627        Monitor Pattern mentioned frequently in the <span class="bold"><strong>Boost.Threads</strong></span>
628        documentation.</p></p>
629</div>
630<div class="biblioentry">
631<a name="threads.bib.Stroustrup"></a><p>[<span class="abbrev"><a name="threads.bib.Stroustrup.abbrev"></a>Stroustrup</span>] <span class="title"><i><a href="http://cseng.aw.com/book/0,3828,0201700735,00.html" target="_top">The C++ Programming Language</a></i>. </span><span class="edition">Special Edition. </span><span class="publisher">Addison-Wesley</span><span class="copyright">Copyright © 2000. </span><span class="isbn">ISBN: 0-201-70073-5. </span><p>The first book a C++ programmer should own. Note that the 3rd edition
632        (and subsequent editions like the Special Edition) has been rewritten to
633        cover the ISO standard language and library.</p></p>
634</div>
635</div>
636</div>
637<table width="100%"><tr>
638<td align="left"><small><p>Last revised: July 07, 2005 at 18:06:26 GMT</p></small></td>
639<td align="right"><small></small></td>
640</tr></table>
641<hr>
642<div class="spirit-nav">
643<a accesskey="p" href="string_algo/credits.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><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="threads/design.html"><img src="images/next.png" alt="Next"></a>
644</div>
645</body>
646</html>
Note: See TracBrowser for help on using the repository browser.