This commit was manufactured by cvs2svn to create tag 'r13beta1'.
[python/dscho.git] / Doc / libthread.tex
blobf7453844dbe5e8f7985f74ab012f6fedc6efbea5
1 \section{Built-in Module \sectcode{thread}}
2 \bimodindex{thread}
4 This module provides low-level primitives for working with multiple
5 threads (a.k.a.\ \dfn{light-weight processes} or \dfn{tasks}) --- multiple
6 threads of control sharing their global data space. For
7 synchronization, simple locks (a.k.a.\ \dfn{mutexes} or \dfn{binary
8 semaphores}) are provided.
10 The module is optional and supported on SGI IRIX 4.x and 5.x and Sun
11 Solaris 2.x systems, as well as on systems that have a PTHREAD
12 implementation (e.g.\ KSR).
14 It defines the following constant and functions:
16 \renewcommand{\indexsubitem}{(in module thread)}
17 \begin{excdesc}{error}
18 Raised on thread-specific errors.
19 \end{excdesc}
21 \begin{funcdesc}{start_new_thread}{func\, arg}
22 Start a new thread. The thread executes the function \var{func}
23 with the argument list \var{arg} (which must be a tuple). When the
24 function returns, the thread silently exits. When the function
25 terminates with an unhandled exception, a stack trace is printed and
26 then the thread exits (but other threads continue to run).
27 \end{funcdesc}
29 \begin{funcdesc}{exit}{}
30 This is a shorthand for \code{thread.exit_thread()}.
31 \end{funcdesc}
33 \begin{funcdesc}{exit_thread}{}
34 Raise the \code{SystemExit} exception. When not caught, this will
35 cause the thread to exit silently.
36 \end{funcdesc}
38 %\begin{funcdesc}{exit_prog}{status}
39 %Exit all threads and report the value of the integer argument
40 %\var{status} as the exit status of the entire program.
41 %\strong{Caveat:} code in pending \code{finally} clauses, in this thread
42 %or in other threads, is not executed.
43 %\end{funcdesc}
45 \begin{funcdesc}{allocate_lock}{}
46 Return a new lock object. Methods of locks are described below. The
47 lock is initially unlocked.
48 \end{funcdesc}
50 \begin{funcdesc}{get_ident}{}
51 Return the `thread identifier' of the current thread. This is a
52 nonzero integer. Its value has no direct meaning; it is intended as a
53 magic cookie to be used e.g. to index a dictionary of thread-specific
54 data. Thread identifiers may be recycled when a thread exits and
55 another thread is created.
56 \end{funcdesc}
58 Lock objects have the following methods:
60 \renewcommand{\indexsubitem}{(lock method)}
61 \begin{funcdesc}{acquire}{\optional{waitflag}}
62 Without the optional argument, this method acquires the lock
63 unconditionally, if necessary waiting until it is released by another
64 thread (only one thread at a time can acquire a lock --- that's their
65 reason for existence), and returns \code{None}. If the integer
66 \var{waitflag} argument is present, the action depends on its value:\
67 if it is zero, the lock is only acquired if it can be acquired
68 immediately without waiting, while if it is nonzero, the lock is
69 acquired unconditionally as before. If an argument is present, the
70 return value is 1 if the lock is acquired successfully, 0 if not.
71 \end{funcdesc}
73 \begin{funcdesc}{release}{}
74 Releases the lock. The lock must have been acquired earlier, but not
75 necessarily by the same thread.
76 \end{funcdesc}
78 \begin{funcdesc}{locked}{}
79 Return the status of the lock:\ 1 if it has been acquired by some
80 thread, 0 if not.
81 \end{funcdesc}
83 {\bf Caveats:}
85 \begin{itemize}
86 \item
87 Threads interact strangely with interrupts: the
88 \code{KeyboardInterrupt} exception will be received by an arbitrary
89 thread. (When the \code{signal} module is available, interrupts
90 always go to the main thread.)
92 \item
93 Calling \code{sys.exit()} or raising the \code{SystemExit} is
94 equivalent to calling \code{thread.exit_thread()}.
96 \item
97 Not all built-in functions that may block waiting for I/O allow other
98 threads to run. (The most popular ones (\code{sleep}, \code{read},
99 \code{select}) work as expected.)
101 \end{itemize}