more pod patches
[p5sagit/p5-mst-13.2.git] / ext / Thread / Thread.pm
CommitLineData
d9bb3666 1package Thread;
2require Exporter;
9426adcd 3use XSLoader ();
17f410f9 4our($VERSION, @ISA, @EXPORT);
52e1cb5e 5
6$VERSION = "1.0";
7
9426adcd 8@ISA = qw(Exporter);
8f4f90ac 9@EXPORT_OK = qw(yield cond_signal cond_broadcast cond_wait async);
734689b1 10
d516a115 11=head1 NAME
12
e7d08fc9 13Thread - manipulate threads in Perl (EXPERIMENTAL, subject to change)
d516a115 14
15=head1 SYNOPSIS
16
17 use Thread;
18
19 my $t = new Thread \&start_sub, @start_args;
20
23a4f76c 21 $result = $t->join;
22 $result = $t->eval;
23 $t->detach;
8dcd6f7b 24 $flags = $t->flags;
bbc7dcd2 25
8dcd6f7b 26 if ($t->done) {
27 $t->join;
28 }
d516a115 29
23a4f76c 30 if($t->equal($another_thread)) {
31 # ...
32 }
d516a115 33
23a4f76c 34 my $tid = Thread->self->tid;
d516a115 35 my $tlist = Thread->list;
36
37 lock($scalar);
23a4f76c 38 yield();
d516a115 39
40 use Thread 'async';
41
d516a115 42=head1 DESCRIPTION
43
2a4bf773 44 WARNING: Threading is an experimental feature. Both the interface
45 and implementation are subject to change drastically. In fact, this
46 documentation describes the flavor of threads that was in version
47 5.005. Perl 5.6.0 and later have the beginnings of support for
48 interpreter threads, which (when finished) is expected to be
49 significantly different from what is described here. The information
50 contained here may therefore soon be obsolete. Use at your own risk!
23a4f76c 51
2a4bf773 52The C<Thread> module provides multithreading support for perl.
e7d08fc9 53
589fe9d5 54=head1 FUNCTIONS
55
56=over 8
57
58=item new \&start_sub
59
60=item new \&start_sub, LIST
61
62C<new> starts a new thread of execution in the referenced subroutine. The
63optional list is passed as parameters to the subroutine. Execution
64continues in both the subroutine and the code after the C<new> call.
65
66C<new Thread> returns a thread object representing the newly created
67thread.
68
69=item lock VARIABLE
70
71C<lock> places a lock on a variable until the lock goes out of scope. If
72the variable is locked by another thread, the C<lock> call will block until
73it's available. C<lock> is recursive, so multiple calls to C<lock> are
74safe--the variable will remain locked until the outermost lock on the
75variable goes out of scope.
76
77Locks on variables only affect C<lock> calls--they do I<not> affect normal
78access to a variable. (Locks on subs are different, and covered in a bit)
79If you really, I<really> want locks to block access, then go ahead and tie
80them to something and manage this yourself. This is done on purpose. While
81managing access to variables is a good thing, perl doesn't force you out of
82its living room...
83
84If a container object, such as a hash or array, is locked, all the elements
85of that container are not locked. For example, if a thread does a C<lock
86@a>, any other thread doing a C<lock($a[12])> won't block.
87
88You may also C<lock> a sub, using C<lock &sub>. Any calls to that sub from
89another thread will block until the lock is released. This behaviour is not
0655b981 90equivalent to declaring the sub with the C<locked> attribute. The C<locked>
91attribute serializes access to a subroutine, but allows different threads
589fe9d5 92non-simultaneous access. C<lock &sub>, on the other hand, will not allow
93I<any> other thread access for the duration of the lock.
94
95Finally, C<lock> will traverse up references exactly I<one> level.
96C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
97
98=item async BLOCK;
99
100C<async> creates a thread to execute the block immediately following
101it. This block is treated as an anonymous sub, and so must have a
102semi-colon after the closing brace. Like C<new Thread>, C<async> returns a
103thread object.
104
105=item Thread->self
106
107The C<Thread-E<gt>self> function returns a thread object that represents
108the thread making the C<Thread-E<gt>self> call.
109
110=item Thread->list
111
112C<Thread-E<gt>list> returns a list of thread objects for all running and
113finished but un-C<join>ed threads.
114
115=item cond_wait VARIABLE
116
117The C<cond_wait> function takes a B<locked> variable as a parameter,
118unlocks the variable, and blocks until another thread does a C<cond_signal>
119or C<cond_broadcast> for that same locked variable. The variable that
120C<cond_wait> blocked on is relocked after the C<cond_wait> is satisfied.
121If there are multiple threads C<cond_wait>ing on the same variable, all but
122one will reblock waiting to reaquire the lock on the variable. (So if
123you're only using C<cond_wait> for synchronization, give up the lock as
124soon as possible)
125
126=item cond_signal VARIABLE
127
128The C<cond_signal> function takes a locked variable as a parameter and
129unblocks one thread that's C<cond_wait>ing on that variable. If more than
130one thread is blocked in a C<cond_wait> on that variable, only one (and
131which one is indeterminate) will be unblocked.
132
133If there are no threads blocked in a C<cond_wait> on the variable, the
134signal is discarded.
135
136=item cond_broadcast VARIABLE
137
7284b4ab 138The C<cond_broadcast> function works similarly to C<cond_signal>.
589fe9d5 139C<cond_broadcast>, though, will unblock B<all> the threads that are blocked
140in a C<cond_wait> on the locked variable, rather than only one.
141
23a4f76c 142=item yield
143
144The C<yield> function allows another thread to take control of the
145CPU. The exact results are implementation-dependent.
146
589fe9d5 147=back
148
149=head1 METHODS
150
151=over 8
152
153=item join
154
155C<join> waits for a thread to end and returns any values the thread exited
156with. C<join> will block until the thread has ended, though it won't block
157if the thread has already terminated.
158
159If the thread being C<join>ed C<die>d, the error it died with will be
160returned at this time. If you don't want the thread performing the C<join>
161to die as well, you should either wrap the C<join> in an C<eval> or use the
162C<eval> thread method instead of C<join>.
163
164=item eval
165
166The C<eval> method wraps an C<eval> around a C<join>, and so waits for a
167thread to exit, passing along any values the thread might have returned.
168Errors, of course, get placed into C<$@>.
169
23a4f76c 170=item detach
171
172C<detach> tells a thread that it is never going to be joined i.e.
173that all traces of its existence can be removed once it stops running.
174Errors in detached threads will not be visible anywhere - if you want
175to catch them, you should use $SIG{__DIE__} or something like that.
176
177=item equal
178
179C<equal> tests whether two thread objects represent the same thread and
180returns true if they do.
181
589fe9d5 182=item tid
183
184The C<tid> method returns the tid of a thread. The tid is a monotonically
185increasing integer assigned when a thread is created. The main thread of a
186program will have a tid of zero, while subsequent threads will have tids
187assigned starting with one.
188
8dcd6f7b 189=item flags
190
191The C<flags> method returns the flags for the thread. This is the
192integer value corresponding to the internal flags for the thread, and
e01a9ca0 193the value may not be all that meaningful to you.
8dcd6f7b 194
195=item done
196
197The C<done> method returns true if the thread you're checking has
198finished, and false otherwise.
199
a45bd81d 200=back
201
589fe9d5 202=head1 LIMITATIONS
203
204The sequence number used to assign tids is a simple integer, and no
205checking is done to make sure the tid isn't currently in use. If a program
206creates more than 2^32 - 1 threads in a single run, threads may be assigned
207duplicate tids. This limitation may be lifted in a future version of Perl.
d516a115 208
209=head1 SEE ALSO
210
0655b981 211L<attributes>, L<Thread::Queue>, L<Thread::Semaphore>, L<Thread::Specific>.
d516a115 212
213=cut
214
734689b1 215#
216# Methods
217#
218
219#
220# Exported functions
221#
222sub async (&) {
223 return new Thread $_[0];
224}
d9bb3666 225
458fb581 226sub eval {
227 return eval { shift->join; };
228}
229
9426adcd 230XSLoader::load 'Thread';
d9bb3666 231
d9bb3666 2321;