blob: fcee360f84d11e85b31a2dcd24d271d84879fef8 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Module \sectcode{thread}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-thread}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003\bimodindex{thread}
4
5This module provides low-level primitives for working with multiple
Guido van Rossum6bb1adc1995-03-13 10:03:32 +00006threads (a.k.a.\ \dfn{light-weight processes} or \dfn{tasks}) --- multiple
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007threads of control sharing their global data space. For
Guido van Rossum6bb1adc1995-03-13 10:03:32 +00008synchronization, simple locks (a.k.a.\ \dfn{mutexes} or \dfn{binary
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00009semaphores}) are provided.
Fred Drake61b04521998-01-20 05:52:23 +000010\index{light-weight processes}
11\index{processes, light-weight}
12\index{binary semaphores}
13\index{semaphores, binary}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000014
Guido van Rossum3572d371997-08-14 19:51:26 +000015The module is optional. It is supported on Windows NT and '95, SGI
16IRIX, Solaris 2.x, as well as on systems that have a POSIX thread
17(a.k.a. ``pthread'') implementation.
Fred Drake61b04521998-01-20 05:52:23 +000018\index{pthreads}
19\indexii{threads}{posix}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020
21It defines the following constant and functions:
22
23\renewcommand{\indexsubitem}{(in module thread)}
24\begin{excdesc}{error}
25Raised on thread-specific errors.
26\end{excdesc}
27
28\begin{funcdesc}{start_new_thread}{func\, arg}
29Start a new thread. The thread executes the function \var{func}
30with the argument list \var{arg} (which must be a tuple). When the
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000031function returns, the thread silently exits. When the function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032terminates with an unhandled exception, a stack trace is printed and
33then the thread exits (but other threads continue to run).
34\end{funcdesc}
35
Guido van Rossum470be141995-03-17 16:07:09 +000036\begin{funcdesc}{exit}{}
37This is a shorthand for \code{thread.exit_thread()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000038\end{funcdesc}
39
Guido van Rossum470be141995-03-17 16:07:09 +000040\begin{funcdesc}{exit_thread}{}
41Raise the \code{SystemExit} exception. When not caught, this will
42cause the thread to exit silently.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000043\end{funcdesc}
44
Guido van Rossum470be141995-03-17 16:07:09 +000045%\begin{funcdesc}{exit_prog}{status}
46%Exit all threads and report the value of the integer argument
47%\var{status} as the exit status of the entire program.
48%\strong{Caveat:} code in pending \code{finally} clauses, in this thread
49%or in other threads, is not executed.
50%\end{funcdesc}
51
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052\begin{funcdesc}{allocate_lock}{}
53Return a new lock object. Methods of locks are described below. The
54lock is initially unlocked.
55\end{funcdesc}
56
Guido van Rossumb8b264b1994-08-12 13:13:50 +000057\begin{funcdesc}{get_ident}{}
58Return the `thread identifier' of the current thread. This is a
59nonzero integer. Its value has no direct meaning; it is intended as a
60magic cookie to be used e.g. to index a dictionary of thread-specific
61data. Thread identifiers may be recycled when a thread exits and
62another thread is created.
63\end{funcdesc}
64
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000065Lock objects have the following methods:
66
67\renewcommand{\indexsubitem}{(lock method)}
Guido van Rossum96628a91995-04-10 11:34:00 +000068\begin{funcdesc}{acquire}{\optional{waitflag}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000069Without the optional argument, this method acquires the lock
70unconditionally, if necessary waiting until it is released by another
71thread (only one thread at a time can acquire a lock --- that's their
72reason for existence), and returns \code{None}. If the integer
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000073\var{waitflag} argument is present, the action depends on its value:\
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000074if it is zero, the lock is only acquired if it can be acquired
75immediately without waiting, while if it is nonzero, the lock is
76acquired unconditionally as before. If an argument is present, the
77return value is 1 if the lock is acquired successfully, 0 if not.
78\end{funcdesc}
79
80\begin{funcdesc}{release}{}
81Releases the lock. The lock must have been acquired earlier, but not
82necessarily by the same thread.
83\end{funcdesc}
84
85\begin{funcdesc}{locked}{}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000086Return the status of the lock:\ 1 if it has been acquired by some
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000087thread, 0 if not.
88\end{funcdesc}
89
Fred Drakeaf8a0151998-01-14 14:51:31 +000090\strong{Caveats:}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000091
92\begin{itemize}
93\item
94Threads interact strangely with interrupts: the
95\code{KeyboardInterrupt} exception will be received by an arbitrary
Fred Drake61b04521998-01-20 05:52:23 +000096thread. (When the \code{signal}\refbimodindex{signal} module is
97available, interrupts always go to the main thread.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000098
99\item
Guido van Rossum3572d371997-08-14 19:51:26 +0000100Calling \code{sys.exit()} or raising the \code{SystemExit} exception is
Guido van Rossum470be141995-03-17 16:07:09 +0000101equivalent to calling \code{thread.exit_thread()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000102
103\item
104Not all built-in functions that may block waiting for I/O allow other
Fred Drake61b04521998-01-20 05:52:23 +0000105threads to run. (The most popular ones (\code{sleep()}, \code{read()},
106\code{select()}) work as expected.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000107
Guido van Rossum3572d371997-08-14 19:51:26 +0000108\item
109It is not possible to interrupt the \code{acquire()} method on a lock
110-- the \code{KeyboardInterrupt} exception will happen after the lock
111has been acquired.
112
113\item
114When the main thread exits, it is system defined whether the other
115threads survive. On SGI IRIX using the native thread implementation,
116they survive. On most other systems, they are killed without
117executing ``try-finally'' clauses or executing object destructors.
Fred Drake61b04521998-01-20 05:52:23 +0000118\indexii{threads}{IRIX}
Guido van Rossum3572d371997-08-14 19:51:26 +0000119
120\item
121When the main thread exits, it doesn't do any of its usual cleanup
122(except that ``try-finally'' clauses are honored), and the standard
123I/O files are not flushed.
124
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000125\end{itemize}