Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libthreading.tex @ 8831:00a6bb65ff29 legacy-trunk
Getting rid of this ancient stuff.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 05 Aug 1998 21:10:09 +0000 |
| parents | 8434e9d00b5a |
| children | acfffe14628e |
| rev | line source |
|---|---|
|
8683
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
1 \section{Standard Module \module{threading}} |
|
8716
760dcf197048
Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents:
8683
diff
changeset
|
2 \declaremodule{standard}{threading} |
|
760dcf197048
Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents:
8683
diff
changeset
|
3 |
| 8767 | 4 \modulesynopsis{Higher-level threading interfaces.} |
|
8716
760dcf197048
Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents:
8683
diff
changeset
|
5 |
|
8683
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
6 |
| 8767 | 7 This module constructs higher-level threading interfaces on top of the |
| 8 lower level \module{thread} module. | |
|
8683
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
9 |
| 8767 | 10 This module is safe for use with \samp{from threading import *}. It |
|
8683
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
11 defines the following functions and objects: |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
12 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
13 \begin{funcdesc}{activeCount}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
14 Return the number of currently active \class{Thread} objects. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
15 The returned count is equal to the length of the list returned by |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
16 \function{enumerate()}. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
17 A function that returns the number of currently active threads. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
18 \end{funcdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
19 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
20 \begin{funcdesc}{Condition}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
21 A factory function that returns a new condition variable object. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
22 A condition variable allows one or more threads to wait until they |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
23 are notified by another thread. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
24 \end{funcdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
25 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
26 \begin{funcdesc}{currentThread}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
27 Return the current \class{Thread} object, corresponding to the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
28 caller's thread of control. If the caller's thread of control was not |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
29 created through the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
30 \module{threading} module, a dummy thread object with limited functionality |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
31 is returned. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
32 \end{funcdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
33 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
34 \begin{funcdesc}{enumerate}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
35 Return a list of all currently active \class{Thread} objects. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
36 The list includes daemonic threads, dummy thread objects created |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
37 by \function{currentThread()}, and the main thread. It excludes terminated |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
38 threads and threads that have not yet been started. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
39 \end{funcdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
40 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
41 \begin{funcdesc}{Event}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
42 A factory function that returns a new event object. An event |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
43 manages a flag that can be set to true with the \method{set()} method and |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
44 reset to false with the \method{clear()} method. The \method{wait()} method blocks |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
45 until the flag is true. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
46 \end{funcdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
47 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
48 \begin{funcdesc}{Lock}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
49 A factory function that returns a new primitive lock object. Once |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
50 a thread has acquired it, subsequent attempts to acquire it block, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
51 until it is released; any thread may release it. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
52 \end{funcdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
53 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
54 \begin{funcdesc}{RLock}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
55 A factory function that returns a new reentrant lock object. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
56 A reentrant lock must be released by the thread that acquired it. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
57 Once a thread has acquired a reentrant lock, the same thread may |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
58 acquire it again without blocking; the thread must release it once |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
59 for each time it has acquired it. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
60 \end{funcdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
61 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
62 \begin{funcdesc}{Semaphore}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
63 A factory function that returns a new semaphore object. A |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
64 semaphore manages a counter representing the number of \method{release()} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
65 calls minus the number of \method{acquire()} calls, plus an initial value. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
66 The \method{acquire()} method blocks if necessary until it can return |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
67 without making the counter negative. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
68 \end{funcdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
69 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
70 \begin{classdesc}{Thread}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
71 A class that represents a thread of control. This class can be safely subclassed in a limited fashion. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
72 \end{classdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
73 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
74 Detailed interfaces for the objects are documented below. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
75 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
76 The design of this module is loosely based on Java's threading model. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
77 However, where Java makes locks and condition variables basic behavior |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
78 of every object, they are separate objects in Python. Python's \class{Thread} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
79 class supports a subset of the behavior of Java's Thread class; |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
80 currently, there are no priorities, no thread groups, and threads |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
81 cannot be destroyed, stopped, suspended, resumed, or interrupted. The |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
82 static methods of Java's Thread class, when implemented, are mapped to |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
83 module-level functions. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
84 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
85 All of the methods described below are executed atomically. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
86 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
87 \subsection{Lock Objects} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
88 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
89 A primitive lock is a synchronization primitive that is not owned |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
90 by a particular thread when locked. In Python, it is currently |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
91 the lowest level synchronization primitive available, implemented |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
92 directly by the \module{thread} extension module. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
93 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
94 A primitive lock is in one of two states, ``locked'' or ``unlocked''. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
95 It is created in the unlocked state. It has two basic methods, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
96 \method{acquire()} and \method{release()}. When the state is |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
97 unlocked, \method{acquire()} changes the state to locked and returns |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
98 immediately. When the state is locked, \method{acquire()} blocks |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
99 until a call to \method{release()} in another thread changes it to |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
100 unlocked, then the \method{acquire()} call resets it to locked and |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
101 returns. The \method{release()} method should only be called in the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
102 locked state; it changes the state to unlocked and returns |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
103 immediately. When more than one thread is blocked in |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
104 \method{acquire()} waiting for the state to turn to unlocked, only one |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
105 thread proceeds when a \method{release()} call resets the state to |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
106 unlocked; which one of the waiting threads proceeds is not defined, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
107 and may vary across implementations. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
108 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
109 All methods are executed atomically. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
110 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
111 \begin{methoddesc}{acquire}{blocking=1} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
112 Acquire a lock, blocking or non-blocking. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
113 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
114 When invoked without arguments, block until the lock is |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
115 unlocked, then set it to locked, and return. There is no |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
116 return value in this case. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
117 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
118 When invoked with the \var{blocking} argument set to true, do the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
119 same thing as when called without arguments, and return true. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
120 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
121 When invoked with the \var{blocking} argument set to false, do not |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
122 block. If a call without an argument would block, return false |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
123 immediately; otherwise, do the same thing as when called |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
124 without arguments, and return true. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
125 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
126 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
127 \begin{methoddesc}{release}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
128 Release a lock. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
129 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
130 When the lock is locked, reset it to unlocked, and return. If |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
131 any other threads are blocked waiting for the lock to become |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
132 unlocked, allow exactly one of them to proceed. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
133 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
134 Do not call this method when the lock is unlocked. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
135 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
136 There is no return value. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
137 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
138 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
139 \subsection{RLock Objects} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
140 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
141 A reentrant lock is a synchronization primitive that may be |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
142 acquired multiple times by the same thread. Internally, it uses |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
143 the concepts of ``owning thread'' and ``recursion level'' in |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
144 addition to the locked/unlocked state used by primitive locks. In |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
145 the locked state, some thread owns the lock; in the unlocked |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
146 state, no thread owns it. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
147 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
148 To lock the lock, a thread calls its \method{acquire()} method; this |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
149 returns once the thread owns the lock. To unlock the lock, a |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
150 thread calls its \method{release()} method. \method{acquire()}/\method{release()} call pairs |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
151 may be nested; only the final \method{release()} (i.e. the \method{release()} of the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
152 outermost pair) resets the lock to unlocked and allows another |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
153 thread blocked in \method{acquire()} to proceed. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
154 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
155 \begin{methoddesc}{acquire}{blocking=1} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
156 Acquire a lock, blocking or non-blocking. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
157 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
158 When invoked without arguments: if this thread already owns |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
159 the lock, increment the recursion level by one, and return |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
160 immediately. Otherwise, if another thread owns the lock, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
161 block until the lock is unlocked. Once the lock is unlocked |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
162 (not owned by any thread), then grab ownership, set the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
163 recursion level to one, and return. If more than one thread |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
164 is blocked waiting until the lock is unlocked, only one at a |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
165 time will be able to grab ownership of the lock. There is no |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
166 return value in this case. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
167 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
168 When invoked with the \var{blocking} argument set to true, do the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
169 same thing as when called without arguments, and return true. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
170 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
171 When invoked with the \var{blocking} argument set to false, do not |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
172 block. If a call without an argument would block, return false |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
173 immediately; otherwise, do the same thing as when called |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
174 without arguments, and return true. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
175 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
176 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
177 \begin{methoddesc}{release}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
178 Release a lock, decrementing the recursion level. If after the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
179 decrement it is zero, reset the lock to unlocked (not owned by any |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
180 thread), and if any other threads are blocked waiting for the lock to |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
181 become unlocked, allow exactly one of them to proceed. If after the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
182 decrement the recursion level is still nonzero, the lock remains |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
183 locked and owned by the calling thread. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
184 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
185 Only call this method when the calling thread owns the lock. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
186 Do not call this method when the lock is unlocked. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
187 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
188 There is no return value. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
189 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
190 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
191 \subsection{Condition Objects} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
192 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
193 A condition variable is always associated with some kind of lock; |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
194 this can be passed in or one will be created by default. (Passing |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
195 one in is useful when several condition variables must share the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
196 same lock.) |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
197 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
198 A condition variable has \method{acquire()} and \method{release()} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
199 methods that call the corresponding methods of the associated lock. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
200 It also has a \method{wait()} method, and \method{notify()} and |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
201 \method{notifyAll()} methods. These three must only be called when |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
202 the calling thread has acquired the lock. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
203 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
204 The \method{wait()} method releases the lock, and then blocks until it |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
205 is awakened by a \method{notify()} or \method{notifyAll()} call for |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
206 the same condition variable in another thread. Once awakened, it |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
207 re-acquires the lock and returns. It is also possible to specify a |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
208 timeout. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
209 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
210 The \method{notify()} method wakes up one of the threads waiting for |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
211 the condition variable, if any are waiting. The \method{notifyAll()} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
212 method wakes up all threads waiting for the condition variable. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
213 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
214 Note: the \method{notify()} and \method{notifyAll()} methods don't |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
215 release the lock; this means that the thread or threads awakened will |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
216 not return from their \method{wait()} call immediately, but only when |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
217 the thread that called \method{notify()} or \method{notifyAll()} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
218 finally relinquishes ownership of the lock. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
219 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
220 Tip: the typical programming style using condition variables uses the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
221 lock to synchronize access to some shared state; threads that are |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
222 interested in a particular change of state call \method{wait()} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
223 repeatedly until they see the desired state, while threads that modify |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
224 the state call \method{notify()} or \method{notifyAll()} when they |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
225 change the state in such a way that it could possibly be a desired |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
226 state for one of the waiters. For example, the following code is a |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
227 generic producer-consumer situation with unlimited buffer capacity: |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
228 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
229 \begin{verbatim} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
230 # Consume one item |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
231 cv.acquire() |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
232 while not an_item_is_available(): |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
233 cv.wait() |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
234 get_an_available_item() |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
235 cv.release() |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
236 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
237 # Produce one item |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
238 cv.acquire() |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
239 make_an_item_available() |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
240 cv.notify() |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
241 cv.release() |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
242 \end{verbatim} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
243 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
244 To choose between \method{notify()} and \method{notifyAll()}, consider |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
245 whether one state change can be interesting for only one or several |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
246 waiting threads. E.g. in a typical producer-consumer situation, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
247 adding one item to the buffer only needs to wake up one consumer |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
248 thread. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
249 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
250 \begin{classdesc}{Condition}{lock=None} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
251 If the \var{lock} argument is given and not \code{None}, it must be a \class{Lock} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
252 or \class{RLock} object, and it is used as the underlying lock. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
253 Otherwise, a new \class{RLock} object is created and used as the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
254 underlying lock. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
255 \end{classdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
256 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
257 \begin{methoddesc}{acquire}{*args} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
258 Acquire the underlying lock. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
259 This method calls the corresponding method on the underlying |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
260 lock; the return value is whatever that method returns. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
261 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
262 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
263 \begin{methoddesc}{release}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
264 Release the underlying lock. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
265 This method calls the corresponding method on the underlying |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
266 lock; there is no return value. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
267 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
268 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
269 \begin{methoddesc}{wait}{timeout=None} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
270 Wait until notified or until a timeout occurs. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
271 This must only be called when the calling thread has acquired the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
272 lock. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
273 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
274 This method releases the underlying lock, and then blocks until it is |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
275 awakened by a \method{notify()} or \method{notifyAll()} call for the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
276 same condition variable in another thread, or until the optional |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
277 timeout occurs. Once awakened or timed out, it re-acquires the lock |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
278 and returns. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
279 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
280 When the timeout argument is present and not \code{None}, it should be a |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
281 floating point number specifying a timeout for the operation in |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
282 seconds (or fractions thereof). |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
283 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
284 When the underlying lock is an \class{RLock}, it is not released using its |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
285 \method{release()} method, since this may not actually unlock the lock |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
286 when it was acquired multiple times recursively. Instead, an |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
287 internal interface of the \class{RLock} class is used, which really unlocks it |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
288 even when it has been recursively acquired several times. Another |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
289 internal interface is then used to restore the recursion level when |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
290 the lock is reacquired. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
291 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
292 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
293 \begin{methoddesc}{notify}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
294 Wake up a thread waiting on this condition, if any. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
295 This must only be called when the calling thread has acquired the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
296 lock. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
297 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
298 This method wakes up one of the threads waiting for the condition |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
299 variable, if any are waiting; it is a no-op if no threads are waiting. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
300 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
301 The current implementation wakes up exactly one thread, if any are |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
302 waiting. However, it's not safe to rely on this behavior. A future, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
303 optimized implementation may occasionally wake up more than one |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
304 thread. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
305 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
306 Note: the awakened thread does not actually return from its |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
307 \method{wait()} call until it can reacquire the lock. Since |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
308 \method{notify()} does not release the lock, its caller should. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
309 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
310 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
311 \begin{methoddesc}{notifyAll}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
312 Wake up all threads waiting on this condition. This method acts like |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
313 \method{notify()}, but wakes up all waiting threads instead of one. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
314 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
315 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
316 \subsection{Semaphore Objects} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
317 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
318 This is one of the oldest synchronization primitives in the history of |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
319 computer science, invented by the early Dutch computer scientist |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
320 Edsger W. Dijkstra (he used \method{P()} and \method{V()} instead of \method{acquire()} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
321 and \method{release()}). |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
322 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
323 A semaphore manages an internal counter which is decremented by each |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
324 \method{acquire()} call and incremented by each \method{release()} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
325 call. The counter can never go below zero; when \method{acquire()} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
326 finds that it is zero, it blocks, waiting until some other thread |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
327 calls \method{release()}. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
328 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
329 \begin{classdesc}{Semaphore}{value=1} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
330 The optional argument gives the initial value for the internal |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
331 counter; it defaults to 1. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
332 \end{classdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
333 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
334 \begin{methoddesc}{acquire}{blocking=1} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
335 Acquire a semaphore. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
336 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
337 When invoked without arguments: if the internal counter is larger than |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
338 zero on entry, decrement it by one and return immediately. If it is |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
339 zero on entry, block, waiting until some other thread has called |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
340 \method{release()} to make it larger than zero. This is done with |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
341 proper interlocking so that if multiple \method{acquire()} calls are |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
342 blocked, \method{release()} will wake exactly one of them up. The |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
343 implementation may pick one at random, so the order in which blocked |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
344 threads are awakened should not be relied on. There is no return |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
345 value in this case. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
346 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
347 When invoked with the \var{blocking} argument set to true, do the same |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
348 thing as when called without arguments, and return true. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
349 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
350 When invoked with the \var{blocking} argument set to false, do not |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
351 block. If a call without an argument would block, return false |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
352 immediately; otherwise, do the same thing as when called without |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
353 arguments, and return true. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
354 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
355 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
356 \begin{methoddesc}{release}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
357 Release a semaphore, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
358 incrementing the internal counter by one. When it was zero on |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
359 entry and another thread is waiting for it to become larger |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
360 than zero again, wake up that thread. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
361 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
362 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
363 \subsection{Event Objects} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
364 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
365 This is one of the simplest mechanisms for communication between |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
366 threads: one thread signals an event and one or more other thread |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
367 are waiting for it. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
368 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
369 An event object manages an internal flag that can be set to true with |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
370 the \method{set()} method and reset to false with the \method{clear()} method. The |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
371 \method{wait()} method blocks until the flag is true. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
372 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
373 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
374 \begin{classdesc}{Event}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
375 The internal flag is initially false. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
376 \end{classdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
377 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
378 \begin{methoddesc}{isSet}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
379 Return true if and only if the internal flag is true. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
380 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
381 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
382 \begin{methoddesc}{set}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
383 Set the internal flag to true. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
384 All threads waiting for it to become true are awakened. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
385 Threads that call \method{wait()} once the flag is true will not block |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
386 at all. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
387 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
388 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
389 \begin{methoddesc}{clear}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
390 Reset the internal flag to false. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
391 Subsequently, threads calling \method{wait()} will block until \method{set()} is |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
392 called to set the internal flag to true again. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
393 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
394 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
395 \begin{methoddesc}{wait}{timeout=None} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
396 Block until the internal flag is true. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
397 If the internal flag is true on entry, return immediately. Otherwise, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
398 block until another thread calls \method{set()} to set the flag to |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
399 true, or until the optional timeout occurs. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
400 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
401 When the timeout argument is present and not \code{None}, it should be a |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
402 floating point number specifying a timeout for the operation in |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
403 seconds (or fractions thereof). |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
404 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
405 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
406 \subsection{Thread Objects} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
407 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
408 This class represents an activity that is run in a separate thread |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
409 of control. There are two ways to specify the activity: by |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
410 passing a callable object to the constructor, or by overriding the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
411 \method{run()} method in a subclass. No other methods (except for the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
412 constructor) should be overridden in a subclass. In other words, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
413 \emph{only} override the \method{__init__()} and \method{run()} methods of this class. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
414 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
415 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
416 Once a thread object is created, its activity must be started by |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
417 calling the thread's \method{start()} method. This invokes the \method{run()} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
418 method in a separate thread of control. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
419 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
420 Once the thread's activity is started, the thread is considered |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
421 'alive' and 'active' (these concepts are almost, but not quite |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
422 exactly, the same; their definition is intentionally somewhat |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
423 vague). It stops being alive and active when its \method{run()} method |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
424 terminates -- either normally, or by raising an unhandled |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
425 exception. The \method{isAlive()} method tests whether the thread is |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
426 alive. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
427 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
428 Other threads can call a thread's \method{join()} method. This blocks the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
429 calling thread until the thread whose \method{join()} method is called |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
430 is terminated. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
431 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
432 A thread has a name. The name can be passed to the constructor, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
433 set with the \method{setName()} method, and retrieved with the \method{getName()} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
434 method. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
435 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
436 A thread can be flagged as a ``daemon thread''. The significance |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
437 of this flag is that the entire Python program exits when only |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
438 daemon threads are left. The initial value is inherited from the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
439 creating thread. The flag can be set with the \method{setDaemon()} method |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
440 and retrieved with the \method{getDaemon()} method. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
441 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
442 There is a ``main thread'' object; this corresponds to the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
443 initial thread of control in the Python program. It is not a |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
444 daemon thread. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
445 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
446 There is the possibility that ``dummy thread objects'' are |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
447 created. These are thread objects corresponding to ``alien |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
448 threads''. These are threads of control started outside the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
449 threading module, e.g. directly from C code. Dummy thread objects |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
450 have limited functionality; they are always considered alive, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
451 active, and daemonic, and cannot be \method{join()}ed. They are never |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
452 deleted, since it is impossible to detect the termination of alien |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
453 threads. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
454 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
455 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
456 \begin{classdesc}{Thread}{group=None, target=None, name=None, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
457 args=(), kwargs={}} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
458 This constructor should always be called with keyword |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
459 arguments. Arguments are: |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
460 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
461 group |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
462 Should be None; reserved for future extension when a |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
463 ThreadGroup class is implemented. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
464 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
465 target |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
466 Callable object to be invoked by the \method{run()} method. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
467 Defaults to None, meaning nothing is called. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
468 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
469 name |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
470 The thread name. By default, a unique name is constructed |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
471 of the form ``Thread-N'' where N is a small decimal |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
472 number. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
473 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
474 args |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
475 Argument tuple for the target invocation. Defaults to (). |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
476 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
477 kwargs |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
478 Keyword argument dictionary for the target invocation. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
479 Defaults to {}. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
480 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
481 If the subclass overrides the constructor, it must make sure |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
482 to invoke the base class constructor (Thread.__init__()) |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
483 before doing anything else to the thread. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
484 \end{classdesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
485 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
486 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
487 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
488 \begin{methoddesc}{start}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
489 Start the thread's activity. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
490 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
491 This must be called at most once per thread object. It |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
492 arranges for the object's \method{run()} method to be invoked in a |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
493 separate thread of control. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
494 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
495 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
496 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
497 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
498 \begin{methoddesc}{run}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
499 Method representing the thread's activity. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
500 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
501 You may override this method in a subclass. The standard |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
502 \method{run()} method invokes the callable object passed to the object's constructor as the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
503 \var{target} argument, if any, with sequential and keyword |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
504 arguments taken from the \var{args} and \var{kwargs} arguments, |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
505 respectively. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
506 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
507 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
508 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
509 \begin{methoddesc}{join}{timeout=None} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
510 Wait until the thread terminates. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
511 This blocks the calling thread until the thread whose \method{join()} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
512 method is called terminates -- either normally or through an |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
513 unhandled exception -- or until the optional timeout occurs. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
514 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
515 When the \var{timeout} argument is present and not \code{None}, it should |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
516 be a floating point number specifying a timeout for the |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
517 operation in seconds (or fractions thereof). |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
518 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
519 A thread can be \method{join()}ed many times. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
520 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
521 A thread cannot join itself because this would cause a |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
522 deadlock. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
523 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
524 It is an error to attempt to \method{join()} a thread before it has |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
525 been started. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
526 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
527 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
528 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
529 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
530 \begin{methoddesc}{getName}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
531 Return the thread's name. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
532 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
533 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
534 \begin{methoddesc}{setName}{name} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
535 Set the thread's name. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
536 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
537 The name is a string used for identification purposes only. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
538 It has no semantics. Multiple threads may be given the same |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
539 name. The initial name is set by the constructor. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
540 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
541 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
542 \begin{methoddesc}{isAlive}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
543 Return whether the thread is alive. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
544 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
545 Roughly, a thread is alive from the moment the \method{start()} method |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
546 returns until its \method{run()} method terminates. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
547 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
548 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
549 \begin{methoddesc}{isDaemon}{} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
550 Return the thread's daemon flag. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
551 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
552 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
553 \begin{methoddesc}{setDaemon}{daemonic} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
554 Set the thread's daemon flag to the Boolean value \var{daemonic}. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
555 This must be called before \method{start()} is called. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
556 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
557 The initial value is inherited from the creating thread. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
558 |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
559 The entire Python program exits when no active non-daemon |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
560 threads are left. |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
561 \end{methoddesc} |
|
a0324a3068e9
Added libthreading.tex ; this is mostly the contents of threading_api.py,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
562 |
