blob: 4208c2d1e62b623886c0a0d79852fd1ac63d4e1e [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
Guido van Rossumb8b264b1994-08-12 13:13:50 +000010The module is optional and supported on SGI IRIX 4.x and 5.x and Sun
11Solaris 2.x systems, as well as on systems that have a PTHREAD
12implementation (e.g. KSR).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000013
14It defines the following constant and functions:
15
16\renewcommand{\indexsubitem}{(in module thread)}
17\begin{excdesc}{error}
18Raised on thread-specific errors.
19\end{excdesc}
20
21\begin{funcdesc}{start_new_thread}{func\, arg}
22Start a new thread. The thread executes the function \var{func}
23with the argument list \var{arg} (which must be a tuple). When the
24function returns, the thread silently exits. When the function raises
25terminates with an unhandled exception, a stack trace is printed and
26then the thread exits (but other threads continue to run).
27\end{funcdesc}
28
29\begin{funcdesc}{exit_thread}{}
30Exit the current thread silently. Other threads continue to run.
31\strong{Caveat:} code in pending \code{finally} clauses is not executed.
32\end{funcdesc}
33
34\begin{funcdesc}{exit_prog}{status}
35Exit all threads and report the value of the integer argument
36\var{status} as the exit status of the entire program.
37\strong{Caveat:} code in pending \code{finally} clauses, in this thread
38or in other threads, is not executed.
39\end{funcdesc}
40
41\begin{funcdesc}{allocate_lock}{}
42Return a new lock object. Methods of locks are described below. The
43lock is initially unlocked.
44\end{funcdesc}
45
Guido van Rossumb8b264b1994-08-12 13:13:50 +000046\begin{funcdesc}{get_ident}{}
47Return the `thread identifier' of the current thread. This is a
48nonzero integer. Its value has no direct meaning; it is intended as a
49magic cookie to be used e.g. to index a dictionary of thread-specific
50data. Thread identifiers may be recycled when a thread exits and
51another thread is created.
52\end{funcdesc}
53
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054Lock objects have the following methods:
55
56\renewcommand{\indexsubitem}{(lock method)}
57\begin{funcdesc}{acquire}{waitflag}
58Without the optional argument, this method acquires the lock
59unconditionally, if necessary waiting until it is released by another
60thread (only one thread at a time can acquire a lock --- that's their
61reason for existence), and returns \code{None}. If the integer
62\var{waitflag} argument is present, the action depends on its value:
63if it is zero, the lock is only acquired if it can be acquired
64immediately without waiting, while if it is nonzero, the lock is
65acquired unconditionally as before. If an argument is present, the
66return value is 1 if the lock is acquired successfully, 0 if not.
67\end{funcdesc}
68
69\begin{funcdesc}{release}{}
70Releases the lock. The lock must have been acquired earlier, but not
71necessarily by the same thread.
72\end{funcdesc}
73
74\begin{funcdesc}{locked}{}
75Return the status of the lock: 1 if it has been acquired by some
76thread, 0 if not.
77\end{funcdesc}
78
79{\bf Caveats:}
80
81\begin{itemize}
82\item
83Threads interact strangely with interrupts: the
84\code{KeyboardInterrupt} exception will be received by an arbitrary
85thread.
86
87\item
88Calling \code{sys.exit(\var{status})} or executing
89\code{raise SystemExit, \var{status}} is almost equivalent to calling
90\code{thread.exit_prog(\var{status})}, except that the former ways of
91exiting the entire program do honor \code{finally} clauses in the
92current thread (but not in other threads).
93
94\item
95Not all built-in functions that may block waiting for I/O allow other
96threads to run, although the most popular ones (\code{sleep},
97\code{read}, \code{select}) work as expected.
98
99\end{itemize}