blob: f7453844dbe5e8f7985f74ab012f6fedc6efbea5 [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
Guido van Rossum6bb1adc1995-03-13 10:03:32 +00005threads (a.k.a.\ \dfn{light-weight processes} or \dfn{tasks}) --- multiple
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00006threads of control sharing their global data space. For
Guido van Rossum6bb1adc1995-03-13 10:03:32 +00007synchronization, simple locks (a.k.a.\ \dfn{mutexes} or \dfn{binary
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008semaphores}) 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
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000012implementation (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
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000024function returns, the thread silently exits. When the function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000025terminates with an unhandled exception, a stack trace is printed and
26then the thread exits (but other threads continue to run).
27\end{funcdesc}
28
Guido van Rossum470be141995-03-17 16:07:09 +000029\begin{funcdesc}{exit}{}
30This is a shorthand for \code{thread.exit_thread()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031\end{funcdesc}
32
Guido van Rossum470be141995-03-17 16:07:09 +000033\begin{funcdesc}{exit_thread}{}
34Raise the \code{SystemExit} exception. When not caught, this will
35cause the thread to exit silently.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000036\end{funcdesc}
37
Guido van Rossum470be141995-03-17 16:07:09 +000038%\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}
44
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000045\begin{funcdesc}{allocate_lock}{}
46Return a new lock object. Methods of locks are described below. The
47lock is initially unlocked.
48\end{funcdesc}
49
Guido van Rossumb8b264b1994-08-12 13:13:50 +000050\begin{funcdesc}{get_ident}{}
51Return the `thread identifier' of the current thread. This is a
52nonzero integer. Its value has no direct meaning; it is intended as a
53magic cookie to be used e.g. to index a dictionary of thread-specific
54data. Thread identifiers may be recycled when a thread exits and
55another thread is created.
56\end{funcdesc}
57
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000058Lock objects have the following methods:
59
60\renewcommand{\indexsubitem}{(lock method)}
Guido van Rossum96628a91995-04-10 11:34:00 +000061\begin{funcdesc}{acquire}{\optional{waitflag}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000062Without the optional argument, this method acquires the lock
63unconditionally, if necessary waiting until it is released by another
64thread (only one thread at a time can acquire a lock --- that's their
65reason for existence), and returns \code{None}. If the integer
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000066\var{waitflag} argument is present, the action depends on its value:\
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000067if it is zero, the lock is only acquired if it can be acquired
68immediately without waiting, while if it is nonzero, the lock is
69acquired unconditionally as before. If an argument is present, the
70return value is 1 if the lock is acquired successfully, 0 if not.
71\end{funcdesc}
72
73\begin{funcdesc}{release}{}
74Releases the lock. The lock must have been acquired earlier, but not
75necessarily by the same thread.
76\end{funcdesc}
77
78\begin{funcdesc}{locked}{}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000079Return the status of the lock:\ 1 if it has been acquired by some
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080thread, 0 if not.
81\end{funcdesc}
82
83{\bf Caveats:}
84
85\begin{itemize}
86\item
87Threads interact strangely with interrupts: the
88\code{KeyboardInterrupt} exception will be received by an arbitrary
Guido van Rossum470be141995-03-17 16:07:09 +000089thread. (When the \code{signal} module is available, interrupts
90always go to the main thread.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000091
92\item
Guido van Rossum470be141995-03-17 16:07:09 +000093Calling \code{sys.exit()} or raising the \code{SystemExit} is
94equivalent to calling \code{thread.exit_thread()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000095
96\item
97Not all built-in functions that may block waiting for I/O allow other
Guido van Rossum470be141995-03-17 16:07:09 +000098threads to run. (The most popular ones (\code{sleep}, \code{read},
99\code{select}) work as expected.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000100
101\end{itemize}