1Thread(3pm) Perl Programmers Reference Guide Thread(3pm)
2
3
4
6 Thread - Manipulate threads in Perl (for old code only)
7
9 The "Thread" module served as the frontend to the old-style thread
10 model, called 5005threads, that was introduced in release 5.005. That
11 model was deprecated, and has been removed in version 5.10.
12
13 For old code and interim backwards compatibility, the "Thread" module
14 has been reworked to function as a frontend for the new interpreter
15 threads (ithreads) model. However, some previous functionality is not
16 available. Further, the data sharing models between the two thread
17 models are completely different, and anything to do with data sharing
18 has to be thought differently. With ithreads, you must explicitly
19 share() variables between the threads.
20
21 You are strongly encouraged to migrate any existing threaded code to
22 the new model (i.e., use the "threads" and "threads::shared" modules)
23 as soon as possible.
24
26 In Perl 5.005, the thread model was that all data is implicitly shared,
27 and shared access to data has to be explicitly synchronized. This
28 model is called 5005threads.
29
30 In Perl 5.6, a new model was introduced in which all is was thread
31 local and shared access to data has to be explicitly declared. This
32 model is called ithreads, for "interpreter threads".
33
34 In Perl 5.6, the ithreads model was not available as a public API; only
35 as an internal API that was available for extension writers, and to
36 implement fork() emulation on Win32 platforms.
37
38 In Perl 5.8, the ithreads model became available through the "threads"
39 module, and the 5005threads model was deprecated.
40
41 In Perl 5.10, the 5005threads model was removed from the Perl
42 interpreter.
43
45 use Thread qw(:DEFAULT async yield);
46
47 my $t = Thread->new(\&start_sub, @start_args);
48
49 $result = $t->join;
50 $t->detach;
51
52 if ($t->done) {
53 $t->join;
54 }
55
56 if($t->equal($another_thread)) {
57 # ...
58 }
59
60 yield();
61
62 my $tid = Thread->self->tid;
63
64 lock($scalar);
65 lock(@array);
66 lock(%hash);
67
68 my @list = Thread->list;
69
71 The "Thread" module provides multithreading support for Perl.
72
74 $thread = Thread->new(\&start_sub)
75 $thread = Thread->new(\&start_sub, LIST)
76 "new" starts a new thread of execution in the referenced
77 subroutine. The optional list is passed as parameters to the
78 subroutine. Execution continues in both the subroutine and the
79 code after the "new" call.
80
81 "Thread->new" returns a thread object representing the newly
82 created thread.
83
84 lock VARIABLE
85 "lock" places a lock on a variable until the lock goes out of
86 scope.
87
88 If the variable is locked by another thread, the "lock" call
89 will block until it's available. "lock" is recursive, so
90 multiple calls to "lock" are safe--the variable will remain
91 locked until the outermost lock on the variable goes out of
92 scope.
93
94 Locks on variables only affect "lock" calls--they do not affect
95 normal access to a variable. (Locks on subs are different, and
96 covered in a bit.) If you really, really want locks to block
97 access, then go ahead and tie them to something and manage this
98 yourself. This is done on purpose. While managing access to
99 variables is a good thing, Perl doesn't force you out of its
100 living room...
101
102 If a container object, such as a hash or array, is locked, all
103 the elements of that container are not locked. For example, if
104 a thread does a "lock @a", any other thread doing a
105 lock($a[12]) won't block.
106
107 Finally, "lock" will traverse up references exactly one level.
108 lock(\$a) is equivalent to lock($a), while lock(\\$a) is not.
109
110 async BLOCK;
111 "async" creates a thread to execute the block immediately
112 following it. This block is treated as an anonymous sub, and
113 so must have a semi-colon after the closing brace. Like
114 "Thread->new", "async" returns a thread object.
115
116 Thread->self
117 The "Thread->self" function returns a thread object that
118 represents the thread making the "Thread->self" call.
119
120 Thread->list
121 Returns a list of all non-joined, non-detached Thread objects.
122
123 cond_wait VARIABLE
124 The "cond_wait" function takes a locked variable as a
125 parameter, unlocks the variable, and blocks until another
126 thread does a "cond_signal" or "cond_broadcast" for that same
127 locked variable. The variable that "cond_wait" blocked on is
128 relocked after the "cond_wait" is satisfied. If there are
129 multiple threads "cond_wait"ing on the same variable, all but
130 one will reblock waiting to re-acquire the lock on the
131 variable. (So if you're only using "cond_wait" for
132 synchronization, give up the lock as soon as possible.)
133
134 cond_signal VARIABLE
135 The "cond_signal" function takes a locked variable as a
136 parameter and unblocks one thread that's "cond_wait"ing on that
137 variable. If more than one thread is blocked in a "cond_wait"
138 on that variable, only one (and which one is indeterminate)
139 will be unblocked.
140
141 If there are no threads blocked in a "cond_wait" on the
142 variable, the signal is discarded.
143
144 cond_broadcast VARIABLE
145 The "cond_broadcast" function works similarly to "cond_signal".
146 "cond_broadcast", though, will unblock all the threads that are
147 blocked in a "cond_wait" on the locked variable, rather than
148 only one.
149
150 yield The "yield" function allows another thread to take control of
151 the CPU. The exact results are implementation-dependent.
152
154 join "join" waits for a thread to end and returns any values the
155 thread exited with. "join" will block until the thread has
156 ended, though it won't block if the thread has already
157 terminated.
158
159 If the thread being "join"ed "die"d, the error it died with
160 will be returned at this time. If you don't want the thread
161 performing the "join" to die as well, you should either wrap
162 the "join" in an "eval" or use the "eval" thread method instead
163 of "join".
164
165 detach "detach" tells a thread that it is never going to be joined
166 i.e. that all traces of its existence can be removed once it
167 stops running. Errors in detached threads will not be visible
168 anywhere - if you want to catch them, you should use
169 $SIG{__DIE__} or something like that.
170
171 equal "equal" tests whether two thread objects represent the same
172 thread and returns true if they do.
173
174 tid The "tid" method returns the tid of a thread. The tid is a
175 monotonically increasing integer assigned when a thread is
176 created. The main thread of a program will have a tid of zero,
177 while subsequent threads will have tids assigned starting with
178 one.
179
180 done The "done" method returns true if the thread you're checking
181 has finished, and false otherwise.
182
184 The following were implemented with 5005threads, but are no longer
185 available with ithreads.
186
187 lock(\&sub)
188 With 5005threads, you could also "lock" a sub such that any
189 calls to that sub from another thread would block until the
190 lock was released.
191
192 Also, subroutines could be declared with the ":locked"
193 attribute which would serialize access to the subroutine, but
194 allowed different threads non-simultaneous access.
195
196 eval The "eval" method wrapped an "eval" around a "join", and so
197 waited for a thread to exit, passing along any values the
198 thread might have returned and placing any errors into $@.
199
200 flags The "flags" method returned the flags for the thread - an
201 integer value corresponding to the internal flags for the
202 thread.
203
205 threads, threads::shared, Thread::Queue, Thread::Semaphore
206
207
208
209perl v5.38.2 2023-11-30 Thread(3pm)