Oops -- Lib/Test should be Lib/test, of course!
[python/dscho.git] / Doc / lib / libthread.tex
blob9e9d8ba0ab8a164ba9d33f9d5ae0c966853a2790
1 \section{Built-in Module \module{thread}}
2 \label{module-thread}
3 \bimodindex{thread}
5 This module provides low-level primitives for working with multiple
6 threads (a.k.a.\ \dfn{light-weight processes} or \dfn{tasks}) --- multiple
7 threads of control sharing their global data space. For
8 synchronization, simple locks (a.k.a.\ \dfn{mutexes} or \dfn{binary
9 semaphores}) are provided.
10 \index{light-weight processes}
11 \index{processes, light-weight}
12 \index{binary semaphores}
13 \index{semaphores, binary}
15 The module is optional. It is supported on Windows NT and '95, SGI
16 IRIX, Solaris 2.x, as well as on systems that have a \POSIX{} thread
17 (a.k.a. ``pthread'') implementation.
18 \index{pthreads}
19 \indexii{threads}{\POSIX{}}
21 It defines the following constant and functions:
23 \begin{excdesc}{error}
24 Raised on thread-specific errors.
25 \end{excdesc}
27 \begin{funcdesc}{start_new_thread}{func, arg}
28 Start a new thread. The thread executes the function \var{func}
29 with the argument list \var{arg} (which must be a tuple). When the
30 function returns, the thread silently exits. When the function
31 terminates with an unhandled exception, a stack trace is printed and
32 then the thread exits (but other threads continue to run).
33 \end{funcdesc}
35 \begin{funcdesc}{exit}{}
36 This is a shorthand for \function{exit_thread()}.
37 \end{funcdesc}
39 \begin{funcdesc}{exit_thread}{}
40 Raise the \exception{SystemExit} exception. When not caught, this
41 will cause the thread to exit silently.
42 \end{funcdesc}
44 %\begin{funcdesc}{exit_prog}{status}
45 %Exit all threads and report the value of the integer argument
46 %\var{status} as the exit status of the entire program.
47 %\strong{Caveat:} code in pending \code{finally} clauses, in this thread
48 %or in other threads, is not executed.
49 %\end{funcdesc}
51 \begin{funcdesc}{allocate_lock}{}
52 Return a new lock object. Methods of locks are described below. The
53 lock is initially unlocked.
54 \end{funcdesc}
56 \begin{funcdesc}{get_ident}{}
57 Return the `thread identifier' of the current thread. This is a
58 nonzero integer. Its value has no direct meaning; it is intended as a
59 magic cookie to be used e.g. to index a dictionary of thread-specific
60 data. Thread identifiers may be recycled when a thread exits and
61 another thread is created.
62 \end{funcdesc}
65 Lock objects have the following methods:
67 \begin{methoddesc}[lock]{acquire}{\optional{waitflag}}
68 Without the optional argument, this method acquires the lock
69 unconditionally, if necessary waiting until it is released by another
70 thread (only one thread at a time can acquire a lock --- that's their
71 reason for existence), and returns \code{None}. If the integer
72 \var{waitflag} argument is present, the action depends on its value:\
73 if it is zero, the lock is only acquired if it can be acquired
74 immediately without waiting, while if it is nonzero, the lock is
75 acquired unconditionally as before. If an argument is present, the
76 return value is \code{1} if the lock is acquired successfully,
77 \code{0} if not.
78 \end{methoddesc}
80 \begin{methoddesc}[lock]{release}{}
81 Releases the lock. The lock must have been acquired earlier, but not
82 necessarily by the same thread.
83 \end{methoddesc}
85 \begin{methoddesc}[lock]{locked}{}
86 Return the status of the lock:\ \code{1} if it has been acquired by
87 some thread, \code{0} if not.
88 \end{methoddesc}
90 \strong{Caveats:}
92 \begin{itemize}
93 \item
94 Threads interact strangely with interrupts: the
95 \exception{KeyboardInterrupt} exception will be received by an
96 arbitrary thread. (When the \module{signal}\refbimodindex{signal}
97 module is available, interrupts always go to the main thread.)
99 \item
100 Calling \function{sys.exit()} or raising the \exception{SystemExit}
101 exception is equivalent to calling \function{exit_thread()}.
103 \item
104 Not all built-in functions that may block waiting for I/O allow other
105 threads to run. (The most popular ones (\function{time.sleep()},
106 \method{\var{file}.read()}, \function{select.select()}) work as
107 expected.)
109 \item
110 It is not possible to interrupt the \method{acquire()} method on a lock
111 --- the \exception{KeyboardInterrupt} exception will happen after the
112 lock has been acquired.
114 \item
115 When the main thread exits, it is system defined whether the other
116 threads survive. On SGI IRIX using the native thread implementation,
117 they survive. On most other systems, they are killed without
118 executing \keyword{try} ... \keyword{finally} clauses or executing
119 object destructors.
120 \indexii{threads}{IRIX}
122 \item
123 When the main thread exits, it does not do any of its usual cleanup
124 (except that \keyword{try} ... \keyword{finally} clauses are honored),
125 and the standard I/O files are not flushed.
127 \end{itemize}