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