annotate Doc/lib/libthread.tex @ 17581:c1937faabb23 2.1

Added support for optional explanation parameter to the \versionadded macro. Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most of the work in a helper function, with the do_cmd_*() wrappers just supplying a portion of the replacement text.
author Fred Drake <fdrake@acm.org>
date Wed, 18 Apr 2001 17:29:14 +0000
parents be3e0d197522
children ebfd87e87884
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8918
acfffe14628e New section header style.
Fred Drake <fdrake@acm.org>
parents: 8716
diff changeset
1 \section{\module{thread} ---
10657
d203cb9ece37 Nits. Reference to signal module becomes a hyperlink.
Fred Drake <fdrake@acm.org>
parents: 9546
diff changeset
2 Multiple threads of control}
d203cb9ece37 Nits. Reference to signal module becomes a hyperlink.
Fred Drake <fdrake@acm.org>
parents: 9546
diff changeset
3
8716
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 8617
diff changeset
4 \declaremodule{builtin}{thread}
8918
acfffe14628e New section header style.
Fred Drake <fdrake@acm.org>
parents: 8716
diff changeset
5 \modulesynopsis{Create multiple threads of control within one interpreter.}
8716
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 8617
diff changeset
6
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 This module provides low-level primitives for working with multiple
2527
815dfaa6a778 small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 1776
diff changeset
9 threads (a.k.a.\ \dfn{light-weight processes} or \dfn{tasks}) --- multiple
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 threads of control sharing their global data space. For
2527
815dfaa6a778 small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 1776
diff changeset
11 synchronization, simple locks (a.k.a.\ \dfn{mutexes} or \dfn{binary
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 semaphores}) are provided.
7141
b81fb67d908d Added a bunch of "concept" index entries.
Fred Drake <fdrake@acm.org>
parents: 7096
diff changeset
13 \index{light-weight processes}
b81fb67d908d Added a bunch of "concept" index entries.
Fred Drake <fdrake@acm.org>
parents: 7096
diff changeset
14 \index{processes, light-weight}
b81fb67d908d Added a bunch of "concept" index entries.
Fred Drake <fdrake@acm.org>
parents: 7096
diff changeset
15 \index{binary semaphores}
b81fb67d908d Added a bunch of "concept" index entries.
Fred Drake <fdrake@acm.org>
parents: 7096
diff changeset
16 \index{semaphores, binary}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
6023
6a25d8110b00 Updated platform specific notes (it is now more common to have this)
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
18 The module is optional. It is supported on Windows NT and '95, SGI
7222
10fdf16f4de9 Consistently use \POSIX{} to denote POSIX. There were at least two different
Fred Drake <fdrake@acm.org>
parents: 7141
diff changeset
19 IRIX, Solaris 2.x, as well as on systems that have a \POSIX{} thread
6023
6a25d8110b00 Updated platform specific notes (it is now more common to have this)
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
20 (a.k.a. ``pthread'') implementation.
7141
b81fb67d908d Added a bunch of "concept" index entries.
Fred Drake <fdrake@acm.org>
parents: 7096
diff changeset
21 \index{pthreads}
7224
d33d45622e67 Oops, missed a \POSIX{} in an index entry.
Fred Drake <fdrake@acm.org>
parents: 7222
diff changeset
22 \indexii{threads}{\POSIX{}}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 It defines the following constant and functions:
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 \begin{excdesc}{error}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 Raised on thread-specific errors.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 \end{excdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
8564
e87d22c7de3c Document optional kwargs argument to start_new_thread(). Also
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
30 \begin{datadesc}{LockType}
e87d22c7de3c Document optional kwargs argument to start_new_thread(). Also
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
31 This is the type of lock objects.
e87d22c7de3c Document optional kwargs argument to start_new_thread(). Also
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
32 \end{datadesc}
e87d22c7de3c Document optional kwargs argument to start_new_thread(). Also
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
33
10657
d203cb9ece37 Nits. Reference to signal module becomes a hyperlink.
Fred Drake <fdrake@acm.org>
parents: 9546
diff changeset
34 \begin{funcdesc}{start_new_thread}{function, args\optional{, kwargs}}
8564
e87d22c7de3c Document optional kwargs argument to start_new_thread(). Also
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
35 Start a new thread. The thread executes the function \var{function}
e87d22c7de3c Document optional kwargs argument to start_new_thread(). Also
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
36 with the argument list \var{args} (which must be a tuple). The
e87d22c7de3c Document optional kwargs argument to start_new_thread(). Also
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
37 optional \var{kwargs} argument specifies a dictionary of keyword
e87d22c7de3c Document optional kwargs argument to start_new_thread(). Also
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
38 arguments. When the
2527
815dfaa6a778 small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 1776
diff changeset
39 function returns, the thread silently exits. When the function
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 terminates with an unhandled exception, a stack trace is printed and
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 then the thread exits (but other threads continue to run).
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
44 \begin{funcdesc}{exit}{}
8564
e87d22c7de3c Document optional kwargs argument to start_new_thread(). Also
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
45 Raise the \exception{SystemExit} exception. When not caught, this
e87d22c7de3c Document optional kwargs argument to start_new_thread(). Also
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
46 will cause the thread to exit silently.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
49 \begin{funcdesc}{exit_thread}{}
8617
46b472ecb0f9 Added \deprecated to exit_thread() function.
Fred Drake <fdrake@acm.org>
parents: 8564
diff changeset
50 \deprecated{1.5.2}{Use \function{exit()}.}
8564
e87d22c7de3c Document optional kwargs argument to start_new_thread(). Also
Guido van Rossum <guido@python.org>
parents: 7877
diff changeset
51 This is an obsolete synonym for \function{exit()}.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
54 %\begin{funcdesc}{exit_prog}{status}
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
55 %Exit all threads and report the value of the integer argument
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
56 %\var{status} as the exit status of the entire program.
10657
d203cb9ece37 Nits. Reference to signal module becomes a hyperlink.
Fred Drake <fdrake@acm.org>
parents: 9546
diff changeset
57 %\strong{Caveat:} code in pending \keyword{finally} clauses, in this thread
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
58 %or in other threads, is not executed.
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
59 %\end{funcdesc}
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
60
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 \begin{funcdesc}{allocate_lock}{}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 Return a new lock object. Methods of locks are described below. The
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 lock is initially unlocked.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
1776
5257af80e161 * Doc/libfuncs.tex: don't use $math$ in description of pow(x,y,z);
Guido van Rossum <guido@python.org>
parents: 1471
diff changeset
66 \begin{funcdesc}{get_ident}{}
5257af80e161 * Doc/libfuncs.tex: don't use $math$ in description of pow(x,y,z);
Guido van Rossum <guido@python.org>
parents: 1471
diff changeset
67 Return the `thread identifier' of the current thread. This is a
5257af80e161 * Doc/libfuncs.tex: don't use $math$ in description of pow(x,y,z);
Guido van Rossum <guido@python.org>
parents: 1471
diff changeset
68 nonzero integer. Its value has no direct meaning; it is intended as a
5257af80e161 * Doc/libfuncs.tex: don't use $math$ in description of pow(x,y,z);
Guido van Rossum <guido@python.org>
parents: 1471
diff changeset
69 magic cookie to be used e.g. to index a dictionary of thread-specific
5257af80e161 * Doc/libfuncs.tex: don't use $math$ in description of pow(x,y,z);
Guido van Rossum <guido@python.org>
parents: 1471
diff changeset
70 data. Thread identifiers may be recycled when a thread exits and
5257af80e161 * Doc/libfuncs.tex: don't use $math$ in description of pow(x,y,z);
Guido van Rossum <guido@python.org>
parents: 1471
diff changeset
71 another thread is created.
5257af80e161 * Doc/libfuncs.tex: don't use $math$ in description of pow(x,y,z);
Guido van Rossum <guido@python.org>
parents: 1471
diff changeset
72 \end{funcdesc}
5257af80e161 * Doc/libfuncs.tex: don't use $math$ in description of pow(x,y,z);
Guido van Rossum <guido@python.org>
parents: 1471
diff changeset
73
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
74
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 Lock objects have the following methods:
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
77 \begin{methoddesc}[lock]{acquire}{\optional{waitflag}}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 Without the optional argument, this method acquires the lock
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 unconditionally, if necessary waiting until it is released by another
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 thread (only one thread at a time can acquire a lock --- that's their
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 reason for existence), and returns \code{None}. If the integer
9546
aa961a46ae9e Minor markup nit to ease conversion processing.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
82 \var{waitflag} argument is present, the action depends on its
aa961a46ae9e Minor markup nit to ease conversion processing.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
83 value: if it is zero, the lock is only acquired if it can be acquired
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 immediately without waiting, while if it is nonzero, the lock is
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 acquired unconditionally as before. If an argument is present, the
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
86 return value is \code{1} if the lock is acquired successfully,
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
87 \code{0} if not.
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
88 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
90 \begin{methoddesc}[lock]{release}{}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 Releases the lock. The lock must have been acquired earlier, but not
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 necessarily by the same thread.
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
93 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
95 \begin{methoddesc}[lock]{locked}{}
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
96 Return the status of the lock:\ \code{1} if it has been acquired by
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
97 some thread, \code{0} if not.
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
98 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
7096
5bb884527446 Consistency: "{\bf ...}" ==> "\strong{...}"
Fred Drake <fdrake@acm.org>
parents: 6023
diff changeset
100 \strong{Caveats:}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 \begin{itemize}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 \item
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 Threads interact strangely with interrupts: the
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
105 \exception{KeyboardInterrupt} exception will be received by an
10657
d203cb9ece37 Nits. Reference to signal module becomes a hyperlink.
Fred Drake <fdrake@acm.org>
parents: 9546
diff changeset
106 arbitrary thread. (When the \refmodule{signal}\refbimodindex{signal}
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
107 module is available, interrupts always go to the main thread.)
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 \item
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
110 Calling \function{sys.exit()} or raising the \exception{SystemExit}
12243
be3e0d197522 Noted by Guido:
Fred Drake <fdrake@acm.org>
parents: 10657
diff changeset
111 exception is equivalent to calling \function{exit()}.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 \item
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 Not all built-in functions that may block waiting for I/O allow other
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
115 threads to run. (The most popular ones (\function{time.sleep()},
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
116 \method{\var{file}.read()}, \function{select.select()}) work as
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
117 expected.)
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
6023
6a25d8110b00 Updated platform specific notes (it is now more common to have this)
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
119 \item
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
120 It is not possible to interrupt the \method{acquire()} method on a lock
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
121 --- the \exception{KeyboardInterrupt} exception will happen after the
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
122 lock has been acquired.
6023
6a25d8110b00 Updated platform specific notes (it is now more common to have this)
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
123
6a25d8110b00 Updated platform specific notes (it is now more common to have this)
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
124 \item
6a25d8110b00 Updated platform specific notes (it is now more common to have this)
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
125 When the main thread exits, it is system defined whether the other
6a25d8110b00 Updated platform specific notes (it is now more common to have this)
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
126 threads survive. On SGI IRIX using the native thread implementation,
6a25d8110b00 Updated platform specific notes (it is now more common to have this)
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
127 they survive. On most other systems, they are killed without
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
128 executing \keyword{try} ... \keyword{finally} clauses or executing
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
129 object destructors.
7141
b81fb67d908d Added a bunch of "concept" index entries.
Fred Drake <fdrake@acm.org>
parents: 7096
diff changeset
130 \indexii{threads}{IRIX}
6023
6a25d8110b00 Updated platform specific notes (it is now more common to have this)
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
131
6a25d8110b00 Updated platform specific notes (it is now more common to have this)
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
132 \item
7817
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
133 When the main thread exits, it does not do any of its usual cleanup
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
134 (except that \keyword{try} ... \keyword{finally} clauses are honored),
461738aaa533 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
135 and the standard I/O files are not flushed.
6023
6a25d8110b00 Updated platform specific notes (it is now more common to have this)
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
136
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 \end{itemize}