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.) You can see your
56 Perl's threading configuration by running C<perl -V> and looking for
57 the I<use...threads> variables, or inside script by C<use Config;>
58 and testing for C<$Config{use5005threads}> and C<$Config{useithreads}>.
60 For old code and interim backwards compatibility, the Thread module
61 has been reworked to function as a frontend for both 5005threads and
64 Note that the compatibility is not complete: because the data sharing
65 models are directly opposed, anything to do with data sharing has to
66 be thought differently. With the ithreads you must explicitly share()
67 variables between the threads.
69 For new code the use of the C<Thread> module is discouraged and
70 the direct use use of the C<threads> and C<threads::shared> modules
71 is encouraged instead.
73 Finally, note that there are many known serious problems with the
74 5005threads, one of the least of which is that regular expression
75 match variables like $1 are not threadsafe, that is, they easily get
76 corrupted by competing threads. Other problems include more insidious
77 data corruption and mysterious crashes. You are seriously urged to
84 my $t = Thread->new(\&start_sub, @start_args);
94 if($t->equal($another_thread)) {
100 my $tid = Thread->self->tid;
106 lock(\&sub); # not available with ithreads
108 $flags = $t->flags; # not available with ithreads
110 my @list = Thread->list; # not available with ithreads
112 unlock(...); # not available with the 5.005 threads
118 The C<Thread> module provides multithreading support for perl.
124 =item $thread = Thread->new(\&start_sub)
126 =item $thread = Thread->new(\&start_sub, LIST)
128 C<new> starts a new thread of execution in the referenced subroutine. The
129 optional list is passed as parameters to the subroutine. Execution
130 continues in both the subroutine and the code after the C<new> call.
132 C<Thread->new> returns a thread object representing the newly created
137 C<lock> places a lock on a variable until the lock goes out of scope
138 (with ithreads you can also explicitly unlock()).
140 If the variable is locked by another thread, the C<lock> call will
141 block until it's available. C<lock> is recursive, so multiple calls
142 to C<lock> are safe--the variable will remain locked until the
143 outermost lock on the variable goes out of scope.
145 Locks on variables only affect C<lock> calls--they do I<not> affect normal
146 access to a variable. (Locks on subs are different, and covered in a bit.)
147 If you really, I<really> want locks to block access, then go ahead and tie
148 them to something and manage this yourself. This is done on purpose.
149 While managing access to variables is a good thing, Perl doesn't force
150 you out of its living room...
152 If a container object, such as a hash or array, is locked, all the
153 elements of that container are not locked. For example, if a thread
154 does a C<lock @a>, any other thread doing a C<lock($a[12])> won't
157 With 5005threads you may also C<lock> a sub, using C<lock &sub>.
158 Any calls to that sub from another thread will block until the lock
159 is released. This behaviour is not equivalent to declaring the sub
160 with the C<locked> attribute. The C<locked> attribute serializes
161 access to a subroutine, but allows different threads non-simultaneous
162 access. C<lock &sub>, on the other hand, will not allow I<any> other
163 thread access for the duration of the lock.
165 Finally, C<lock> will traverse up references exactly I<one> level.
166 C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
170 C<async> creates a thread to execute the block immediately following
171 it. This block is treated as an anonymous sub, and so must have a
172 semi-colon after the closing brace. Like C<Thread->new>, C<async>
173 returns a thread object.
177 The C<Thread-E<gt>self> function returns a thread object that represents
178 the thread making the C<Thread-E<gt>self> call.
180 =item cond_wait VARIABLE
182 The C<cond_wait> function takes a B<locked> variable as
183 a parameter, unlocks the variable, and blocks until another thread
184 does a C<cond_signal> or C<cond_broadcast> for that same locked
185 variable. The variable that C<cond_wait> blocked on is relocked
186 after the C<cond_wait> is satisfied. If there are multiple threads
187 C<cond_wait>ing on the same variable, all but one will reblock waiting
188 to reaquire the lock on the variable. (So if you're only using
189 C<cond_wait> for synchronization, give up the lock as soon as
192 =item cond_signal VARIABLE
194 The C<cond_signal> function takes a locked variable as a parameter and
195 unblocks one thread that's C<cond_wait>ing on that variable. If more than
196 one thread is blocked in a C<cond_wait> on that variable, only one (and
197 which one is indeterminate) will be unblocked.
199 If there are no threads blocked in a C<cond_wait> on the variable,
200 the signal is discarded.
202 =item cond_broadcast VARIABLE
204 The C<cond_broadcast> function works similarly to C<cond_signal>.
205 C<cond_broadcast>, though, will unblock B<all> the threads that are
206 blocked in a C<cond_wait> on the locked variable, rather than only
211 The C<yield> function allows another thread to take control of the
212 CPU. The exact results are implementation-dependent.
222 C<join> waits for a thread to end and returns any values the thread
223 exited with. C<join> will block until the thread has ended, though
224 it won't block if the thread has already terminated.
226 If the thread being C<join>ed C<die>d, the error it died with will
227 be returned at this time. If you don't want the thread performing
228 the C<join> to die as well, you should either wrap the C<join> in
229 an C<eval> or use the C<eval> thread method instead of C<join>.
233 The C<eval> method wraps an C<eval> around a C<join>, and so waits for
234 a thread to exit, passing along any values the thread might have returned.
235 Errors, of course, get placed into C<$@>. (Not available with ithreads.)
239 C<detach> tells a thread that it is never going to be joined i.e.
240 that all traces of its existence can be removed once it stops running.
241 Errors in detached threads will not be visible anywhere - if you want
242 to catch them, you should use $SIG{__DIE__} or something like that.
246 C<equal> tests whether two thread objects represent the same thread and
247 returns true if they do.
251 The C<tid> method returns the tid of a thread. The tid is
252 a monotonically increasing integer assigned when a thread is
253 created. The main thread of a program will have a tid of zero,
254 while subsequent threads will have tids assigned starting with one.
258 The C<flags> method returns the flags for the thread. This is the
259 integer value corresponding to the internal flags for the thread,
260 and the value may not be all that meaningful to you.
261 (Not available with ithreads.)
265 The C<done> method returns true if the thread you're checking has
266 finished, and false otherwise. (Not available with ithreads.)
272 The sequence number used to assign tids is a simple integer, and no
273 checking is done to make sure the tid isn't currently in use. If a
274 program creates more than 2**32 - 1 threads in a single run, threads
275 may be assigned duplicate tids. This limitation may be lifted in
276 a future version of Perl.
280 L<threads::shared> (not available with 5005threads)
282 L<attributes>, L<Thread::Queue>, L<Thread::Semaphore>,
283 L<Thread::Specific> (not available with ithreads)
296 return Thread->new($_[0]);
300 return eval { shift->join; };
304 print $_[0], " unimplemented with ",
305 $Config{useithreads} ? "ithreads" : "5005threads", "\n";
312 *{"Thread::$m"} = sub { unimplemented $m };
318 XSLoader::load 'threads';
319 for my $m (qw(new join detach yield self tid equal)) {
321 *{"Thread::$m"} = \&{"threads::$m"};
323 XSLoader::load 'threads::shared';
324 for my $m (qw(cond_signal cond_broadcast cond_wait unlock share)) {
326 *{"Thread::$m"} = \&{"threads::shared::${m}_enabled"};
328 # trying to unimplement eval gives redefined warning
329 unimplement(qw(list done flags));
330 } elsif ($othreads) {
331 XSLoader::load 'Thread';
332 unimplement(qw(unlock));
335 Carp::croak("This Perl has neither ithreads not 5005threads");