7 $VERSION = eval $VERSION;
11 if (! $Config{useithreads}) {
12 die("This Perl not built to support threads\n");
20 our @ISA = qw(Exporter threads);
21 our @EXPORT = qw(cond_wait cond_broadcast cond_signal);
22 our @EXPORT_OK = qw(async yield);
24 sub async (&) { return Thread->new(shift); }
26 sub done { return ! shift->is_running(); }
28 sub eval { die("'eval' not implemented with 'ithreads'\n"); };
29 sub flags { die("'flags' not implemented with 'ithreads'\n"); };
37 Thread - Manipulate threads in Perl (for old code only)
41 The C<Thread> module served as the frontend to the old-style thread model,
42 called I<5005threads>, that was introduced in release 5.005. That model was
43 deprecated, and has been removed in version 5.10.
45 For old code and interim backwards compatibility, the C<Thread> module has
46 been reworked to function as a frontend for the new interpreter threads
47 (I<ithreads>) model. However, some previous functionality is not available.
48 Further, the data sharing models between the two thread models are completely
49 different, and anything to do with data sharing has to be thought differently.
50 With I<ithreads>, you must explicitly C<share()> variables between the
53 You are strongly encouraged to migrate any existing threaded code to the new
54 model (i.e., use the C<threads> and C<threads::shared> modules) as soon as
59 In Perl 5.005, the thread model was that all data is implicitly shared, and
60 shared access to data has to be explicitly synchronized. This model is called
63 In Perl 5.6, a new model was introduced in which all is was thread local and
64 shared access to data has to be explicitly declared. This model is called
65 I<ithreads>, for "interpreter threads".
67 In Perl 5.6, the I<ithreads> model was not available as a public API; only as
68 an internal API that was available for extension writers, and to implement
69 fork() emulation on Win32 platforms.
71 In Perl 5.8, the I<ithreads> model became available through the C<threads>
72 module, and the I<5005threads> model was deprecated.
74 In Perl 5.10, the I<5005threads> model was removed from the Perl interpreter.
78 use Thread qw(:DEFAULT async yield);
80 my $t = Thread->new(\&start_sub, @start_args);
89 if($t->equal($another_thread)) {
95 my $tid = Thread->self->tid;
101 my @list = Thread->list;
105 The C<Thread> module provides multithreading support for Perl.
111 =item $thread = Thread->new(\&start_sub)
113 =item $thread = Thread->new(\&start_sub, LIST)
115 C<new> starts a new thread of execution in the referenced subroutine. The
116 optional list is passed as parameters to the subroutine. Execution
117 continues in both the subroutine and the code after the C<new> call.
119 C<Thread->new> returns a thread object representing the newly created
124 C<lock> places a lock on a variable until the lock goes out of scope.
126 If the variable is locked by another thread, the C<lock> call will
127 block until it's available. C<lock> is recursive, so multiple calls
128 to C<lock> are safe--the variable will remain locked until the
129 outermost lock on the variable goes out of scope.
131 Locks on variables only affect C<lock> calls--they do I<not> affect normal
132 access to a variable. (Locks on subs are different, and covered in a bit.)
133 If you really, I<really> want locks to block access, then go ahead and tie
134 them to something and manage this yourself. This is done on purpose.
135 While managing access to variables is a good thing, Perl doesn't force
136 you out of its living room...
138 If a container object, such as a hash or array, is locked, all the
139 elements of that container are not locked. For example, if a thread
140 does a C<lock @a>, any other thread doing a C<lock($a[12])> won't
143 Finally, C<lock> will traverse up references exactly I<one> level.
144 C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
148 C<async> creates a thread to execute the block immediately following
149 it. This block is treated as an anonymous sub, and so must have a
150 semi-colon after the closing brace. Like C<Thread->new>, C<async>
151 returns a thread object.
155 The C<Thread-E<gt>self> function returns a thread object that represents
156 the thread making the C<Thread-E<gt>self> call.
160 Returns a list of all non-joined, non-detached Thread objects.
162 =item cond_wait VARIABLE
164 The C<cond_wait> function takes a B<locked> variable as
165 a parameter, unlocks the variable, and blocks until another thread
166 does a C<cond_signal> or C<cond_broadcast> for that same locked
167 variable. The variable that C<cond_wait> blocked on is relocked
168 after the C<cond_wait> is satisfied. If there are multiple threads
169 C<cond_wait>ing on the same variable, all but one will reblock waiting
170 to reaquire the lock on the variable. (So if you're only using
171 C<cond_wait> for synchronization, give up the lock as soon as
174 =item cond_signal VARIABLE
176 The C<cond_signal> function takes a locked variable as a parameter and
177 unblocks one thread that's C<cond_wait>ing on that variable. If more than
178 one thread is blocked in a C<cond_wait> on that variable, only one (and
179 which one is indeterminate) will be unblocked.
181 If there are no threads blocked in a C<cond_wait> on the variable,
182 the signal is discarded.
184 =item cond_broadcast VARIABLE
186 The C<cond_broadcast> function works similarly to C<cond_signal>.
187 C<cond_broadcast>, though, will unblock B<all> the threads that are
188 blocked in a C<cond_wait> on the locked variable, rather than only
193 The C<yield> function allows another thread to take control of the
194 CPU. The exact results are implementation-dependent.
204 C<join> waits for a thread to end and returns any values the thread
205 exited with. C<join> will block until the thread has ended, though
206 it won't block if the thread has already terminated.
208 If the thread being C<join>ed C<die>d, the error it died with will
209 be returned at this time. If you don't want the thread performing
210 the C<join> to die as well, you should either wrap the C<join> in
211 an C<eval> or use the C<eval> thread method instead of C<join>.
215 C<detach> tells a thread that it is never going to be joined i.e.
216 that all traces of its existence can be removed once it stops running.
217 Errors in detached threads will not be visible anywhere - if you want
218 to catch them, you should use $SIG{__DIE__} or something like that.
222 C<equal> tests whether two thread objects represent the same thread and
223 returns true if they do.
227 The C<tid> method returns the tid of a thread. The tid is
228 a monotonically increasing integer assigned when a thread is
229 created. The main thread of a program will have a tid of zero,
230 while subsequent threads will have tids assigned starting with one.
234 The C<done> method returns true if the thread you're checking has
235 finished, and false otherwise.
241 The following were implemented with I<5005threads>, but are no longer
242 available with I<ithreads>.
248 With 5005threads, you could also C<lock> a sub such that any calls to that sub
249 from another thread would block until the lock was released.
251 Also, subroutines could be declared with the C<:locked> attribute which would
252 serialize access to the subroutine, but allowed different threads
253 non-simultaneous access.
257 The C<eval> method wrapped an C<eval> around a C<join>, and so waited for a
258 thread to exit, passing along any values the thread might have returned and
259 placing any errors into C<$@>.
263 The C<flags> method returned the flags for the thread - an integer value
264 corresponding to the internal flags for the thread.
270 L<threads>, L<threads::shared>, L<Thread::Queue>, L<Thread::Semaphore>