12 $ithreads = $Config{useithreads};
13 $othreads = $Config{use5005threads};
18 our($VERSION, @ISA, @EXPORT, @EXPORT_OK);
24 @EXPORT = qw(share cond_wait cond_broadcast cond_signal unlock)
26 @EXPORT_OK = qw(cond_signal cond_broadcast cond_wait);
28 push @EXPORT_OK, qw(async yield);
33 Thread - manipulate threads in Perl
37 Perl has two thread models.
39 In Perl 5.005 the thread model was that all data is implicitly shared
40 and shared access to data has to be explicitly synchronized.
41 This model is called "5005threads".
43 In Perl 5.6 a new model was introduced in which all is was thread
44 local and shared access to data has to be explicitly declared.
45 This model is called "ithreads", for "interpreter threads".
47 In Perl 5.6 the ithreads model was not available as a public API,
48 only as an internal API that was available for extension writers,
49 and to implement fork() emulation on Win32 platforms.
51 In Perl 5.8 the ithreads model became available through the C<threads>
54 Neither model is configured by default into Perl (except, as mentioned
55 above, in Win32 ithreads are always available.)
57 For backwards compatibility, the Thread module has been reworked
58 to function as a frontend for both 5005threads and ithreads.
59 Note that the compatibility is not complete: because the data sharing
60 models are directly opposed, anything to do with data sharing has to
61 be thought differently. With the ithreads you must explicitly share()
62 variables between the threads.
64 Finally, note that there are many known serious problems with the
65 5005threads, one of the least of which is that regular expression
66 match variables like $1 are not threadsafe, that is, they easily get
67 corrupted by competing threads. Other problems include more insidious
68 data corruption and mysterious crashes. You are seriously urged to
75 my $t = Thread->new(\&start_sub, @start_args);
85 if($t->equal($another_thread)) {
91 my $tid = Thread->self->tid;
97 lock(\&sub); # not available with ithreads
99 $flags = $t->flags; # not available with ithreads
101 my @list = Thread->list; # not available with ithreads
103 unlock(...); # not available with the 5.005 threads
109 The C<Thread> module provides multithreading support for perl.
115 =item $thread = Thread->new(\&start_sub)
117 =item $thread = Thread->new(\&start_sub, LIST)
119 C<new> starts a new thread of execution in the referenced subroutine. The
120 optional list is passed as parameters to the subroutine. Execution
121 continues in both the subroutine and the code after the C<new> call.
123 C<Thread->new> returns a thread object representing the newly created
128 C<lock> places a lock on a variable until the lock goes out of scope
129 (with ithreads you can also explicitly unlock()).
131 If the variable is locked by another thread, the C<lock> call will
132 block until it's available. C<lock> is recursive, so multiple calls
133 to C<lock> are safe--the variable will remain locked until the
134 outermost lock on the variable goes out of scope.
136 Locks on variables only affect C<lock> calls--they do I<not> affect normal
137 access to a variable. (Locks on subs are different, and covered in a bit.)
138 If you really, I<really> want locks to block access, then go ahead and tie
139 them to something and manage this yourself. This is done on purpose.
140 While managing access to variables is a good thing, Perl doesn't force
141 you out of its living room...
143 If a container object, such as a hash or array, is locked, all the
144 elements of that container are not locked. For example, if a thread
145 does a C<lock @a>, any other thread doing a C<lock($a[12])> won't
148 With 5005threads you may also C<lock> a sub, using C<lock &sub>.
149 Any calls to that sub from another thread will block until the lock
150 is released. This behaviour is not equivalent to declaring the sub
151 with the C<locked> attribute. The C<locked> attribute serializes
152 access to a subroutine, but allows different threads non-simultaneous
153 access. C<lock &sub>, on the other hand, will not allow I<any> other
154 thread access for the duration of the lock.
156 Finally, C<lock> will traverse up references exactly I<one> level.
157 C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
161 C<async> creates a thread to execute the block immediately following
162 it. This block is treated as an anonymous sub, and so must have a
163 semi-colon after the closing brace. Like C<Thread->new>, C<async>
164 returns a thread object.
168 The C<Thread-E<gt>self> function returns a thread object that represents
169 the thread making the C<Thread-E<gt>self> call.
171 =item cond_wait VARIABLE
173 The C<cond_wait> function takes a B<locked> variable as
174 a parameter, unlocks the variable, and blocks until another thread
175 does a C<cond_signal> or C<cond_broadcast> for that same locked
176 variable. The variable that C<cond_wait> blocked on is relocked
177 after the C<cond_wait> is satisfied. If there are multiple threads
178 C<cond_wait>ing on the same variable, all but one will reblock waiting
179 to reaquire the lock on the variable. (So if you're only using
180 C<cond_wait> for synchronization, give up the lock as soon as
183 =item cond_signal VARIABLE
185 The C<cond_signal> function takes a locked variable as a parameter and
186 unblocks one thread that's C<cond_wait>ing on that variable. If more than
187 one thread is blocked in a C<cond_wait> on that variable, only one (and
188 which one is indeterminate) will be unblocked.
190 If there are no threads blocked in a C<cond_wait> on the variable,
191 the signal is discarded.
193 =item cond_broadcast VARIABLE
195 The C<cond_broadcast> function works similarly to C<cond_signal>.
196 C<cond_broadcast>, though, will unblock B<all> the threads that are
197 blocked in a C<cond_wait> on the locked variable, rather than only
202 The C<yield> function allows another thread to take control of the
203 CPU. The exact results are implementation-dependent.
213 C<join> waits for a thread to end and returns any values the thread
214 exited with. C<join> will block until the thread has ended, though
215 it won't block if the thread has already terminated.
217 If the thread being C<join>ed C<die>d, the error it died with will
218 be returned at this time. If you don't want the thread performing
219 the C<join> to die as well, you should either wrap the C<join> in
220 an C<eval> or use the C<eval> thread method instead of C<join>.
224 The C<eval> method wraps an C<eval> around a C<join>, and so waits for
225 a thread to exit, passing along any values the thread might have returned.
226 Errors, of course, get placed into C<$@>. (Not available with ithreads.)
230 C<detach> tells a thread that it is never going to be joined i.e.
231 that all traces of its existence can be removed once it stops running.
232 Errors in detached threads will not be visible anywhere - if you want
233 to catch them, you should use $SIG{__DIE__} or something like that.
237 C<equal> tests whether two thread objects represent the same thread and
238 returns true if they do.
242 The C<tid> method returns the tid of a thread. The tid is
243 a monotonically increasing integer assigned when a thread is
244 created. The main thread of a program will have a tid of zero,
245 while subsequent threads will have tids assigned starting with one.
249 The C<flags> method returns the flags for the thread. This is the
250 integer value corresponding to the internal flags for the thread,
251 and the value may not be all that meaningful to you.
252 (Not available with ithreads.)
256 The C<done> method returns true if the thread you're checking has
257 finished, and false otherwise. (Not available with ithreads.)
263 The sequence number used to assign tids is a simple integer, and no
264 checking is done to make sure the tid isn't currently in use. If a
265 program creates more than 2**32 - 1 threads in a single run, threads
266 may be assigned duplicate tids. This limitation may be lifted in
267 a future version of Perl.
271 L<threads::shared> (not available with 5005threads)
273 L<attributes>, L<Thread::Queue>, L<Thread::Semaphore>,
274 L<Thread::Specific> (not available with ithreads)
287 return Thread->new($_[0]);
291 return eval { shift->join; };
295 print $_[0], " unimplemented with ",
296 $Config{useithreads} ? "ithreads" : "5005threads", "\n";
303 *{"Thread::$m"} = sub { unimplemented $m };
309 XSLoader::load 'threads';
310 for my $m (qw(new join detach yield self tid equal)) {
312 *{"Thread::$m"} = \&{"threads::$m"};
314 XSLoader::load 'threads::shared';
315 for my $m (qw(cond_signal cond_broadcast cond_wait unlock share)) {
317 *{"Thread::$m"} = \&{"threads::shared::${m}_enabled"};
319 # trying to unimplement eval gives redefined warning
320 unimplement(qw(list done flags));
321 } elsif ($othreads) {
322 XSLoader::load 'Thread';
323 unimplement(qw(unlock));
326 Carp::croak("This Perl has neither ithreads not 5005threads");