📄 thread.pm
字号:
package Thread;use strict;use warnings;no warnings 'redefine';our $VERSION = '3.02';$VERSION = eval $VERSION;BEGIN { use Config; if (! $Config{useithreads}) { die("This Perl not built to support threads\n"); }}use threads 'yield';use threads::shared;require Exporter;our @ISA = qw(Exporter threads);our @EXPORT = qw(cond_wait cond_broadcast cond_signal);our @EXPORT_OK = qw(async yield);sub async (&;@) { return Thread->new(shift); }sub done { return ! shift->is_running(); }sub eval { die("'eval' not implemented with 'ithreads'\n"); };sub flags { die("'flags' not implemented with 'ithreads'\n"); };1;__END__=head1 NAMEThread - Manipulate threads in Perl (for old code only)=head1 DEPRECATEDThe C<Thread> module served as the frontend to the old-style thread model,called I<5005threads>, that was introduced in release 5.005. That model wasdeprecated, and has been removed in version 5.10.For old code and interim backwards compatibility, the C<Thread> module hasbeen reworked to function as a frontend for the new interpreter threads(I<ithreads>) model. However, some previous functionality is not available.Further, the data sharing models between the two thread models are completelydifferent, and anything to do with data sharing has to be thought differently.With I<ithreads>, you must explicitly C<share()> variables between thethreads.You are strongly encouraged to migrate any existing threaded code to the newmodel (i.e., use the C<threads> and C<threads::shared> modules) as soon aspossible.=head1 HISTORYIn Perl 5.005, the thread model was that all data is implicitly shared, andshared access to data has to be explicitly synchronized. This model is calledI<5005threads>.In Perl 5.6, a new model was introduced in which all is was thread local andshared access to data has to be explicitly declared. This model is calledI<ithreads>, for "interpreter threads".In Perl 5.6, the I<ithreads> model was not available as a public API; only asan internal API that was available for extension writers, and to implementfork() emulation on Win32 platforms.In Perl 5.8, the I<ithreads> model became available through the C<threads>module, and the I<5005threads> model was deprecated.In Perl 5.10, the I<5005threads> model was removed from the Perl interpreter.=head1 SYNOPSIS use Thread qw(:DEFAULT async yield); my $t = Thread->new(\&start_sub, @start_args); $result = $t->join; $t->detach; if ($t->done) { $t->join; } if($t->equal($another_thread)) { # ... } yield(); my $tid = Thread->self->tid; lock($scalar); lock(@array); lock(%hash); my @list = Thread->list;=head1 DESCRIPTIONThe C<Thread> module provides multithreading support for Perl.=head1 FUNCTIONS=over 8=item $thread = Thread->new(\&start_sub)=item $thread = Thread->new(\&start_sub, LIST)C<new> starts a new thread of execution in the referenced subroutine. Theoptional list is passed as parameters to the subroutine. Executioncontinues in both the subroutine and the code after the C<new> call.C<Thread->new> returns a thread object representing the newly createdthread.=item lock VARIABLEC<lock> places a lock on a variable until the lock goes out of scope.If the variable is locked by another thread, the C<lock> call willblock until it's available. C<lock> is recursive, so multiple callsto C<lock> are safe--the variable will remain locked until theoutermost lock on the variable goes out of scope.Locks on variables only affect C<lock> calls--they do I<not> affect normalaccess to a variable. (Locks on subs are different, and covered in a bit.)If you really, I<really> want locks to block access, then go ahead and tiethem to something and manage this yourself. This is done on purpose.While managing access to variables is a good thing, Perl doesn't forceyou out of its living room...If a container object, such as a hash or array, is locked, all theelements of that container are not locked. For example, if a threaddoes a C<lock @a>, any other thread doing a C<lock($a[12])> won'tblock.Finally, C<lock> will traverse up references exactly I<one> level.C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.=item async BLOCK;C<async> creates a thread to execute the block immediately followingit. This block is treated as an anonymous sub, and so must have asemi-colon after the closing brace. Like C<Thread->new>, C<async>returns a thread object.=item Thread->selfThe C<Thread-E<gt>self> function returns a thread object that representsthe thread making the C<Thread-E<gt>self> call.=item Thread->listReturns a list of all non-joined, non-detached Thread objects.=item cond_wait VARIABLEThe C<cond_wait> function takes a B<locked> variable asa parameter, unlocks the variable, and blocks until another threaddoes a C<cond_signal> or C<cond_broadcast> for that same lockedvariable. The variable that C<cond_wait> blocked on is relockedafter the C<cond_wait> is satisfied. If there are multiple threadsC<cond_wait>ing on the same variable, all but one will reblock waitingto reaquire the lock on the variable. (So if you're only usingC<cond_wait> for synchronization, give up the lock as soon aspossible.)=item cond_signal VARIABLEThe C<cond_signal> function takes a locked variable as a parameter andunblocks one thread that's C<cond_wait>ing on that variable. If more thanone thread is blocked in a C<cond_wait> on that variable, only one (andwhich one is indeterminate) will be unblocked.If there are no threads blocked in a C<cond_wait> on the variable,the signal is discarded.=item cond_broadcast VARIABLEThe C<cond_broadcast> function works similarly to C<cond_signal>.C<cond_broadcast>, though, will unblock B<all> the threads that areblocked in a C<cond_wait> on the locked variable, rather than onlyone.=item yieldThe C<yield> function allows another thread to take control of theCPU. The exact results are implementation-dependent.=back=head1 METHODS=over 8=item joinC<join> waits for a thread to end and returns any values the threadexited with. C<join> will block until the thread has ended, thoughit won't block if the thread has already terminated.If the thread being C<join>ed C<die>d, the error it died with willbe returned at this time. If you don't want the thread performingthe C<join> to die as well, you should either wrap the C<join> inan C<eval> or use the C<eval> thread method instead of C<join>.=item detachC<detach> tells a thread that it is never going to be joined i.e.that all traces of its existence can be removed once it stops running.Errors in detached threads will not be visible anywhere - if you wantto catch them, you should use $SIG{__DIE__} or something like that.=item equalC<equal> tests whether two thread objects represent the same thread andreturns true if they do.=item tidThe C<tid> method returns the tid of a thread. The tid isa monotonically increasing integer assigned when a thread iscreated. The main thread of a program will have a tid of zero,while subsequent threads will have tids assigned starting with one.=item doneThe C<done> method returns true if the thread you're checking hasfinished, and false otherwise.=back=head1 DEFUNCTThe following were implemented with I<5005threads>, but are no longeravailable with I<ithreads>.=over 8=item lock(\&sub)With 5005threads, you could also C<lock> a sub such that any calls to that subfrom another thread would block until the lock was released.Also, subroutines could be declared with the C<:locked> attribute which wouldserialize access to the subroutine, but allowed different threadsnon-simultaneous access.=item evalThe C<eval> method wrapped an C<eval> around a C<join>, and so waited for athread to exit, passing along any values the thread might have returned andplacing any errors into C<$@>.=item flagsThe C<flags> method returned the flags for the thread - an integer valuecorresponding to the internal flags for the thread.=back=head1 SEE ALSOL<threads>, L<threads::shared>, L<Thread::Queue>, L<Thread::Semaphore>=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -