blob: c836615f44ac898b29cf5fa17ae604add85125d0 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Module \sectcode{thread}}
2\bimodindex{thread}
3
4This module provides low-level primitives for working with multiple
5threads (a.k.a. \dfn{light-weight processes} or \dfn{tasks}) --- multiple
6threads of control sharing their global data space. For
7synchronization, simple locks (a.k.a. \dfn{mutexes} or \dfn{binary
8semaphores}) are provided.
9
10The module is optional and supported on SGI and Sun Sparc systems only.
11
12It defines the following constant and functions:
13
14\renewcommand{\indexsubitem}{(in module thread)}
15\begin{excdesc}{error}
16Raised on thread-specific errors.
17\end{excdesc}
18
19\begin{funcdesc}{start_new_thread}{func\, arg}
20Start a new thread. The thread executes the function \var{func}
21with the argument list \var{arg} (which must be a tuple). When the
22function returns, the thread silently exits. When the function raises
23terminates with an unhandled exception, a stack trace is printed and
24then the thread exits (but other threads continue to run).
25\end{funcdesc}
26
27\begin{funcdesc}{exit_thread}{}
28Exit the current thread silently. Other threads continue to run.
29\strong{Caveat:} code in pending \code{finally} clauses is not executed.
30\end{funcdesc}
31
32\begin{funcdesc}{exit_prog}{status}
33Exit all threads and report the value of the integer argument
34\var{status} as the exit status of the entire program.
35\strong{Caveat:} code in pending \code{finally} clauses, in this thread
36or in other threads, is not executed.
37\end{funcdesc}
38
39\begin{funcdesc}{allocate_lock}{}
40Return a new lock object. Methods of locks are described below. The
41lock is initially unlocked.
42\end{funcdesc}
43
44Lock objects have the following methods:
45
46\renewcommand{\indexsubitem}{(lock method)}
47\begin{funcdesc}{acquire}{waitflag}
48Without the optional argument, this method acquires the lock
49unconditionally, if necessary waiting until it is released by another
50thread (only one thread at a time can acquire a lock --- that's their
51reason for existence), and returns \code{None}. If the integer
52\var{waitflag} argument is present, the action depends on its value:
53if it is zero, the lock is only acquired if it can be acquired
54immediately without waiting, while if it is nonzero, the lock is
55acquired unconditionally as before. If an argument is present, the
56return value is 1 if the lock is acquired successfully, 0 if not.
57\end{funcdesc}
58
59\begin{funcdesc}{release}{}
60Releases the lock. The lock must have been acquired earlier, but not
61necessarily by the same thread.
62\end{funcdesc}
63
64\begin{funcdesc}{locked}{}
65Return the status of the lock: 1 if it has been acquired by some
66thread, 0 if not.
67\end{funcdesc}
68
69{\bf Caveats:}
70
71\begin{itemize}
72\item
73Threads interact strangely with interrupts: the
74\code{KeyboardInterrupt} exception will be received by an arbitrary
75thread.
76
77\item
78Calling \code{sys.exit(\var{status})} or executing
79\code{raise SystemExit, \var{status}} is almost equivalent to calling
80\code{thread.exit_prog(\var{status})}, except that the former ways of
81exiting the entire program do honor \code{finally} clauses in the
82current thread (but not in other threads).
83
84\item
85Not all built-in functions that may block waiting for I/O allow other
86threads to run, although the most popular ones (\code{sleep},
87\code{read}, \code{select}) work as expected.
88
89\end{itemize}