This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Documentation added.
[perl5.git] / ext / threads / threads.pm
CommitLineData
47ba8780
AB
1package threads;
2
73e09c8f 3use 5.007_003;
47ba8780
AB
4use strict;
5use warnings;
73e09c8f
JH
6use Config;
7
8BEGIN {
9 unless ($Config{useithreads}) {
10 my @caller = caller(2);
11 die <<EOF;
12$caller[1] line $caller[2]:
13
14This Perl hasn't been configured and built properly for the threads
15module to work. (The 'useithreads' configuration option hasn't been used.)
16
17Having threads support requires all of Perl and all of the modules in
18the Perl installation to be rebuilt, it is not just a question of adding
19the threads module. (In other words, threaded and non-threaded Perls
20are binary incompatible.)
21
22If you want to the use the threads module, please contact the people
23who built your Perl.
24
25Cannot continue, aborting.
26EOF
27 }
28}
47ba8780 29
68795e93 30use overload
43d3ddbe 31 '==' => \&equal,
47ba8780
AB
32 'fallback' => 1;
33
47ba8780
AB
34#use threads::Shared;
35
dab065ea
AB
36BEGIN {
37 warn "Warning, threads::shared has already been loaded. ".
38 "To enable shared variables for these modules 'use threads' ".
39 "must be called before any of those modules are loaded\n"
40 if($threads::shared::threads_shared);
41}
42
47ba8780
AB
43require Exporter;
44require DynaLoader;
45
47ba8780
AB
46our @ISA = qw(Exporter DynaLoader);
47
48our %EXPORT_TAGS = ( all => [qw()]);
49
50our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
51
52our @EXPORT = qw(
dcb6ccbc 53async
47ba8780 54);
678a9b6c 55our $VERSION = '0.99';
47ba8780 56
47ba8780 57
43d3ddbe 58sub equal {
47ba8780
AB
59 return 1 if($_[0]->tid() == $_[1]->tid());
60 return 0;
61}
62
dcb6ccbc
AB
63sub async (&;@) {
64 my $cref = shift;
65 return threads->new($cref,@_);
66}
67
8222d950 68$threads::threads = 1;
47ba8780
AB
69
70bootstrap threads $VERSION;
71
68795e93
NIS
72# why document 'new' then use 'create' in the tests!
73*create = \&new;
74
47ba8780
AB
75# Preloaded methods go here.
76
771;
78__END__
79
80=head1 NAME
81
82threads - Perl extension allowing use of interpreter based threads from perl
83
84=head1 SYNOPSIS
85
47ba8780
AB
86use threads;
87
88sub start_thread {
89 print "Thread started\n";
90}
91
9c4972d9 92my $thread = threads->create("start_thread","argument");
47ba8780 93
9c4972d9 94$thread->create(sub { print "I am a thread"},"argument");
47ba8780
AB
95
96$thread->join();
97
98$thread->detach();
99
100$thread = threads->self();
101
11c51ed3
AB
102threads->tid();
103threads->self->tid();
104
105$thread->tid();
47ba8780 106
f9dff5f5
AB
107threads->yield();
108
678a9b6c
AB
109threads->list();
110
47ba8780
AB
111=head1 DESCRIPTION
112
43d3ddbe
JH
113Perl 5.6 introduced something called interpreter threads. Interpreter
114threads are different from "5005threads" (the thread model of Perl
1155.005) by creating a new perl interpreter per thread and not sharing
116any data or state between threads.
11c51ed3 117
43d3ddbe
JH
118Prior to perl 5.8 this has only been available to people embedding
119perl and for emulating fork() on windows.
11c51ed3 120
43d3ddbe
JH
121The threads API is loosely based on the old Thread.pm API. It is very
122important to note that variables are not shared between threads, all
123variables are per default thread local. To use shared variables one
124must use threads::shared.
11c51ed3 125
43d3ddbe
JH
126It is also important to note that you preferably enable threads by
127doing C<use threads> as early as possible and that it is not possible
dab065ea
AB
128to enable threading inside an eval ""; In particular, if you are
129intending to share variables with threads::shared, you must
130C<use threads> before you C<use threads::shared> and threads will emit
131a warning if you do it the other way around.
47ba8780
AB
132
133=over
134
9c4972d9 135=item $thread = threads->create(function, LIST)
47ba8780 136
ad91d581
JH
137This will create a new thread with the entry point function and give
138it LIST as parameters. It will return the corresponding threads
139object.
47ba8780 140
11c51ed3 141=item $thread->join
47ba8780 142
43d3ddbe
JH
143This will wait for the corresponding thread to join. When it finishes
144join will return the return values of the entry point function. If a
678a9b6c 145thread has been detached, an error will be thrown..
47ba8780 146
11c51ed3 147=item $thread->detach
47ba8780 148
43d3ddbe
JH
149Will throw away the return value from the thread and make it
150non-joinable.
47ba8780
AB
151
152=item threads->self
153
154This will return the object for the current thread.
155
11c51ed3 156=item $thread->tid
47ba8780 157
678a9b6c
AB
158This will return the id of the thread. threads->tid() is a quick way
159to get current thread id if you don't have your thread handy.
47ba8780 160
f9dff5f5
AB
161=item threads->yield();
162
163This will tell the OS to let this thread yield CPU time to other threads.
164However this is highly depending on the underlying thread implmentation.
165
678a9b6c
AB
166=item threads->list();
167
168This will return a list of all non joined, non detached threads.
169
47ba8780
AB
170=back
171
e4f9f4fe
JH
172=head1 WARNINGS
173
174=over 4
175
176=item Cleanup skipped %d active threads
177
178The main thread exited while there were still other threads running.
4f5d0762
JH
179This is not a good sign: you should either explicitly join the threads,
180or somehow be certain that all the non-main threads have finished.
e4f9f4fe
JH
181
182=back
47ba8780 183
678a9b6c
AB
184=head1 BUGS / TODO
185
186The current implmentation of threads has been an attempt to get
187a correct threading system working that could be built on,
188and optimized, in newer versions of perl.
189
190Current the overhead of creating a thread is rather large,
191also the cost of returning values can be large. These are areas
192were there most likely will be work done to optimize what data
193that needs to be cloned.
47ba8780
AB
194
195=over
196
678a9b6c
AB
197=item Parent-Child threads.
198
199On some platforms it might not be possible to destroy "parent"
200threads while there are still existing child "threads".
201
202This will be possibly be fixed in later versions of perl.
203
204=item tid is I32
205
206The tid is a 32 bit integer, it can potentially overflow.
207This might be fixed in a later version of perl.
47ba8780 208
678a9b6c 209=item Returning objects
47ba8780 210
678a9b6c
AB
211When you return an object the entire stash that the object is blessed
212as well. This will lead to a large memory usage.
213The ideal situation would be to detect the original stash if it existed.
214
215=item PERL_OLD_SIGNALS are not threadsafe, will not be.
47ba8780
AB
216
217=back
218
219=head1 AUTHOR and COPYRIGHT
220
11c51ed3 221Arthur Bergman E<lt>arthur at contiller.seE<gt>
47ba8780 222
43d3ddbe 223threads is released under the same license as Perl.
47ba8780 224
68795e93 225Thanks to
47ba8780 226
68795e93 227Richard Soderberg E<lt>rs at crystalflame.netE<gt>
ad91d581 228Helping me out tons, trying to find reasons for races and other weird bugs!
47ba8780 229
ad91d581
JH
230Simon Cozens E<lt>simon at brecon.co.ukE<gt>
231Being there to answer zillions of annoying questions
47ba8780 232
ad91d581 233Rocco Caputo E<lt>troc at netrus.netE<gt>
47ba8780 234
ad91d581 235Vipul Ved Prakash E<lt>mail at vipul.netE<gt>
47ba8780
AB
236Helping with debugging.
237
238please join perl-ithreads@perl.org for more information
239
b5807cdb 240
11c51ed3 241
47ba8780
AB
242
243=head1 SEE ALSO
244
11c51ed3 245L<perl>, L<threads::shared>, L<perlcall>, L<perlembed>, L<perlguts>
47ba8780
AB
246
247=cut