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