9 unless ($Config{useithreads}) {
10 my @caller = caller(2);
12 $caller[1] line $caller[2]:
14 This Perl hasn't been configured and built properly for the threads
15 module to work. (The 'useithreads' configuration option hasn't been used.)
17 Having threads support requires all of Perl and all of the modules in
18 the Perl installation to be rebuilt, it is not just a question of adding
19 the threads module. (In other words, threaded and non-threaded Perls
20 are binary incompatible.)
22 If you want to the use the threads module, please contact the people
25 Cannot continue, aborting.
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);
46 our @ISA = qw(Exporter DynaLoader);
48 our %EXPORT_TAGS = ( all => [qw(yield)]);
50 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
55 our $VERSION = '0.99';
59 return 1 if($_[0]->tid() == $_[1]->tid());
65 return threads->new($cref,@_);
68 $threads::threads = 1;
70 bootstrap threads $VERSION;
72 # why document 'new' then use 'create' in the tests!
75 # Preloaded methods go here.
82 threads - Perl extension allowing use of interpreter based threads from perl
89 print "Thread started\n";
92 my $thread = threads->create("start_thread","argument");
93 my $thread2 = $thread->create(sub { print "I am a thread"},"argument");
94 my $thread3 = async { foreach (@files) { ... } };
99 $thread = threads->self();
103 threads->self->tid();
111 Perl 5.6 introduced something called interpreter threads. Interpreter
112 threads are different from "5005threads" (the thread model of Perl
113 5.005) by creating a new perl interpreter per thread and not sharing
114 any data or state between threads.
116 Prior to perl 5.8 this has only been available to people embedding
117 perl and for emulating fork() on windows.
119 The threads API is loosely based on the old Thread.pm API. It is very
120 important to note that variables are not shared between threads, all
121 variables are per default thread local. To use shared variables one
122 must use threads::shared.
124 It is also important to note that you must enable threads by
125 doing C<use threads> as early as possible and that it is not possible
126 to enable threading inside an eval ""; In particular, if you are
127 intending to share variables with threads::shared, you must
128 C<use threads> before you C<use threads::shared> and threads will emit
129 a warning if you do it the other way around.
133 =item $thread = threads->create(function, LIST)
135 This will create a new thread with the entry point function and give
136 it LIST as parameters. It will return the corresponding threads
137 object. The new() method is an alias for create().
141 This will wait for the corresponding thread to join. When the thread finishes,
142 join() will return the return values of the entry point function. If the
143 thread has been detached, an error will be thrown. If the program
144 exits without all other threads having been either joined or detached,
145 then a warning will be issued. (A program exits either because one of its
146 threads explicitly calls exit(), or in the case of the main thread, reaches
147 the end of the main program file.)
149 =item $thread->detach
151 Will make the thread unjoinable, and cause any eventual return value to be
156 This will return the thread object for the current thread.
160 This will return the id of the thread. Thread IDs are integers, with the
161 main thread in a program being 0. Currently Perl assigns a unique tid to
162 every thread ever created in your program, assigning the first thread to
163 be created a tid of 1, and increasing the tid by 1 for each new thread
166 NB the class method C<< threads->tid() >> is a quick way to get the
167 current thread id if you don't have your thread object handy.
169 =item threads->yield();
171 This is a suggestion to the OS to let this thread yield CPU time to other
172 threads. What actually happens is highly dependent upon the underlying
173 thread implementation.
175 You may do C<use threads qw(yield)> then use just a bare C<yield> in your
178 =item threads->list();
180 This will return a list of all non joined, non detached threads.
184 C<async> creates a thread to execute the block immediately following
185 it. This block is treated as an anonymous sub, and so must have a
186 semi-colon after the closing brace. Like C<< threads->new >>, C<async>
187 returns a thread object.
195 =item A thread exited while %d other threads were still running
197 A thread (not necessarily the main thread) exited while there were
198 still other threads running. Usually it's a good idea to first collect
199 the return values of the created threads by joining them, and only then
200 exit from then main thread.
206 The current implementation of threads has been an attempt to get
207 a correct threading system working that could be built on,
208 and optimized, in newer versions of perl.
210 Currently the overhead of creating a thread is rather large,
211 also the cost of returning values can be large. These are areas
212 were there most likely will be work done to optimize what data
213 that needs to be cloned.
217 =item Parent-Child threads.
219 On some platforms it might not be possible to destroy "parent"
220 threads while there are still existing child "threads".
222 This will be possibly be fixed in later versions of perl.
226 The tid is a 32 bit integer, it can potentially overflow.
227 This might be fixed in a later version of perl.
229 =item Returning objects
231 When you return an object the entire stash that the object is blessed
232 as well. This will lead to a large memory usage.
233 The ideal situation would be to detect the original stash if it existed.
235 =item PERL_OLD_SIGNALS are not threadsafe, will not be.
239 =head1 AUTHOR and COPYRIGHT
241 Arthur Bergman E<lt>arthur at contiller.seE<gt>
243 threads is released under the same license as Perl.
247 Richard Soderberg E<lt>rs at crystalflame.netE<gt>
248 Helping me out tons, trying to find reasons for races and other weird bugs!
250 Simon Cozens E<lt>simon at brecon.co.ukE<gt>
251 Being there to answer zillions of annoying questions
253 Rocco Caputo E<lt>troc at netrus.netE<gt>
255 Vipul Ved Prakash E<lt>mail at vipul.netE<gt>
256 Helping with debugging.
258 please join perl-ithreads@perl.org for more information
262 L<perl>, L<threads::shared>, L<perlcall>, L<perlembed>, L<perlguts>