Bump version number to 2.4.2 to pick up the latest minor bug fixes.
[python/dscho.git] / Doc / lib / libthread.tex
blob227dd3d4ce1ea4296417058851680a02508997e0
1 \section{\module{thread} ---
2 Multiple threads of control}
4 \declaremodule{builtin}{thread}
5 \modulesynopsis{Create multiple threads of control within one interpreter.}
8 This module provides low-level primitives for working with multiple
9 threads (a.k.a.\ \dfn{light-weight processes} or \dfn{tasks}) --- multiple
10 threads of control sharing their global data space. For
11 synchronization, simple locks (a.k.a.\ \dfn{mutexes} or \dfn{binary
12 semaphores}) are provided.
13 \index{light-weight processes}
14 \index{processes, light-weight}
15 \index{binary semaphores}
16 \index{semaphores, binary}
18 The module is optional. It is supported on Windows NT and '95, SGI
19 IRIX, Solaris 2.x, as well as on systems that have a \POSIX{} thread
20 (a.k.a. ``pthread'') implementation.
21 \index{pthreads}
22 \indexii{threads}{\POSIX}
24 It defines the following constant and functions:
26 \begin{excdesc}{error}
27 Raised on thread-specific errors.
28 \end{excdesc}
30 \begin{datadesc}{LockType}
31 This is the type of lock objects.
32 \end{datadesc}
34 \begin{funcdesc}{start_new_thread}{function, args\optional{, kwargs}}
35 Start a new thread and return its identifier. The thread executes the function
36 \var{function} with the argument list \var{args} (which must be a tuple). The
37 optional \var{kwargs} argument specifies a dictionary of keyword arguments.
38 When the function returns, the thread silently exits. When the function
39 terminates with an unhandled exception, a stack trace is printed and
40 then the thread exits (but other threads continue to run).
41 \end{funcdesc}
43 \begin{funcdesc}{exit}{}
44 Raise the \exception{SystemExit} exception. When not caught, this
45 will cause the thread to exit silently.
46 \end{funcdesc}
48 \begin{funcdesc}{exit_thread}{}
49 \deprecated{1.5.2}{Use \function{exit()}.}
50 This is an obsolete synonym for \function{exit()}.
51 \end{funcdesc}
53 %\begin{funcdesc}{exit_prog}{status}
54 %Exit all threads and report the value of the integer argument
55 %\var{status} as the exit status of the entire program.
56 %\strong{Caveat:} code in pending \keyword{finally} clauses, in this thread
57 %or in other threads, is not executed.
58 %\end{funcdesc}
60 \begin{funcdesc}{allocate_lock}{}
61 Return a new lock object. Methods of locks are described below. The
62 lock is initially unlocked.
63 \end{funcdesc}
65 \begin{funcdesc}{get_ident}{}
66 Return the `thread identifier' of the current thread. This is a
67 nonzero integer. Its value has no direct meaning; it is intended as a
68 magic cookie to be used e.g. to index a dictionary of thread-specific
69 data. Thread identifiers may be recycled when a thread exits and
70 another thread is created.
71 \end{funcdesc}
74 Lock objects have the following methods:
76 \begin{methoddesc}[lock]{acquire}{\optional{waitflag}}
77 Without the optional argument, this method acquires the lock
78 unconditionally, if necessary waiting until it is released by another
79 thread (only one thread at a time can acquire a lock --- that's their
80 reason for existence), and returns \code{None}. If the integer
81 \var{waitflag} argument is present, the action depends on its
82 value: if it is zero, the lock is only acquired if it can be acquired
83 immediately without waiting, while if it is nonzero, the lock is
84 acquired unconditionally as before. If an argument is present, the
85 return value is \code{True} if the lock is acquired successfully,
86 \code{False} if not.
87 \end{methoddesc}
89 \begin{methoddesc}[lock]{release}{}
90 Releases the lock. The lock must have been acquired earlier, but not
91 necessarily by the same thread.
92 \end{methoddesc}
94 \begin{methoddesc}[lock]{locked}{}
95 Return the status of the lock:\ \code{True} if it has been acquired by
96 some thread, \code{False} if not.
97 \end{methoddesc}
99 \strong{Caveats:}
101 \begin{itemize}
102 \item
103 Threads interact strangely with interrupts: the
104 \exception{KeyboardInterrupt} exception will be received by an
105 arbitrary thread. (When the \refmodule{signal}\refbimodindex{signal}
106 module is available, interrupts always go to the main thread.)
108 \item
109 Calling \function{sys.exit()} or raising the \exception{SystemExit}
110 exception is equivalent to calling \function{exit()}.
112 \item
113 Not all built-in functions that may block waiting for I/O allow other
114 threads to run. (The most popular ones (\function{time.sleep()},
115 \method{\var{file}.read()}, \function{select.select()}) work as
116 expected.)
118 \item
119 It is not possible to interrupt the \method{acquire()} method on a lock
120 --- the \exception{KeyboardInterrupt} exception will happen after the
121 lock has been acquired.
123 \item
124 When the main thread exits, it is system defined whether the other
125 threads survive. On SGI IRIX using the native thread implementation,
126 they survive. On most other systems, they are killed without
127 executing \keyword{try} ... \keyword{finally} clauses or executing
128 object destructors.
129 \indexii{threads}{IRIX}
131 \item
132 When the main thread exits, it does not do any of its usual cleanup
133 (except that \keyword{try} ... \keyword{finally} clauses are honored),
134 and the standard I/O files are not flushed.
136 \end{itemize}