📄 threads::shared.3
字号:
\& print("\e$var is not shared\en");\& }.Ve.IP "lock \s-1VARIABLE\s0" 4.IX Item "lock VARIABLE"\&\f(CW\*(C`lock\*(C'\fR places a lock on a variable until the lock goes out of scope. If thevariable is locked by another thread, the \f(CW\*(C`lock\*(C'\fR call will block until it'savailable. Multiple calls to \f(CW\*(C`lock\*(C'\fR by the same thread from withindynamically nested scopes are safe \*(-- the variable will remain locked untilthe outermost lock on the variable goes out of scope..SpLocking a container object, such as a hash or array, doesn't lock the elementsof that container. For example, if a thread does a \f(CW\*(C`lock(@a)\*(C'\fR, any otherthread doing a \f(CW\*(C`lock($a[12])\*(C'\fR won't block..Sp\&\f(CW\*(C`lock()\*(C'\fR follows references exactly \fIone\fR level. \f(CW\*(C`lock(\e$a)\*(C'\fR is equivalentto \f(CW\*(C`lock($a)\*(C'\fR, while \f(CW\*(C`lock(\e\e$a)\*(C'\fR is not..SpNote that you cannot explicitly unlock a variable; you can only wait for thelock to go out of scope. This is most easily accomplished by locking thevariable inside a block..Sp.Vb 7\& my $var :shared;\& {\& lock($var);\& # $var is locked from here to the end of the block\& ...\& }\& # $var is now unlocked.Ve.SpIf you need more fine-grained control over shared variable access, seeThread::Semaphore..IP "cond_wait \s-1VARIABLE\s0" 4.IX Item "cond_wait VARIABLE".PD 0.IP "cond_wait \s-1CONDVAR\s0, \s-1LOCKVAR\s0" 4.IX Item "cond_wait CONDVAR, LOCKVAR".PDThe \f(CW\*(C`cond_wait\*(C'\fR function takes a \fBlocked\fR variable as a parameter, unlocksthe variable, and blocks until another thread does a \f(CW\*(C`cond_signal\*(C'\fR or\&\f(CW\*(C`cond_broadcast\*(C'\fR for that same locked variable. The variable that\&\f(CW\*(C`cond_wait\*(C'\fR blocked on is relocked after the \f(CW\*(C`cond_wait\*(C'\fR is satisfied. Ifthere are multiple threads \f(CW\*(C`cond_wait\*(C'\fRing on the same variable, all but onewill re-block waiting to reacquire the lock on the variable. (So if you're onlyusing \f(CW\*(C`cond_wait\*(C'\fR for synchronisation, give up the lock as soon as possible).The two actions of unlocking the variable and entering the blocked wait stateare atomic, the two actions of exiting from the blocked wait state andre-locking the variable are not..SpIn its second form, \f(CW\*(C`cond_wait\*(C'\fR takes a shared, \fBunlocked\fR variable followedby a shared, \fBlocked\fR variable. The second variable is unlocked and threadexecution suspended until another thread signals the first variable..SpIt is important to note that the variable can be notified even if no thread\&\f(CW\*(C`cond_signal\*(C'\fR or \f(CW\*(C`cond_broadcast\*(C'\fR on the variable. It is thereforeimportant to check the value of the variable and go back to waiting if therequirement is not fulfilled. For example, to pause until a shared counterdrops to zero:.Sp.Vb 1\& { lock($counter); cond_wait($count) until $counter == 0; }.Ve.IP "cond_timedwait \s-1VARIABLE\s0, \s-1ABS_TIMEOUT\s0" 4.IX Item "cond_timedwait VARIABLE, ABS_TIMEOUT".PD 0.IP "cond_timedwait \s-1CONDVAR\s0, \s-1ABS_TIMEOUT\s0, \s-1LOCKVAR\s0" 4.IX Item "cond_timedwait CONDVAR, ABS_TIMEOUT, LOCKVAR".PDIn its two-argument form, \f(CW\*(C`cond_timedwait\*(C'\fR takes a \fBlocked\fR variable and anabsolute timeout as parameters, unlocks the variable, and blocks until thetimeout is reached or another thread signals the variable. A false value isreturned if the timeout is reached, and a true value otherwise. In eithercase, the variable is re-locked upon return..SpLike \f(CW\*(C`cond_wait\*(C'\fR, this function may take a shared, \fBlocked\fR variable as anadditional parameter; in this case the first parameter is an \fBunlocked\fRcondition variable protected by a distinct lock variable..SpAgain like \f(CW\*(C`cond_wait\*(C'\fR, waking up and reacquiring the lock are not atomic,and you should always check your desired condition after this functionreturns. Since the timeout is an absolute value, however, it does not have tobe recalculated with each pass:.Sp.Vb 6\& lock($var);\& my $abs = time() + 15;\& until ($ok = desired_condition($var)) {\& last if !cond_timedwait($var, $abs);\& }\& # we got it if $ok, otherwise we timed out!.Ve.IP "cond_signal \s-1VARIABLE\s0" 4.IX Item "cond_signal VARIABLE"The \f(CW\*(C`cond_signal\*(C'\fR function takes a \fBlocked\fR variable as a parameter andunblocks one thread that's \f(CW\*(C`cond_wait\*(C'\fRing on that variable. If more than onethread is blocked in a \f(CW\*(C`cond_wait\*(C'\fR on that variable, only one (and which oneis indeterminate) will be unblocked..SpIf there are no threads blocked in a \f(CW\*(C`cond_wait\*(C'\fR on the variable, the signalis discarded. By always locking before signaling, you can (with care), avoidsignaling before another thread has entered \fIcond_wait()\fR..Sp\&\f(CW\*(C`cond_signal\*(C'\fR will normally generate a warning if you attempt to use it on anunlocked variable. On the rare occasions where doing this may be sensible, youcan suppress the warning with:.Sp.Vb 1\& { no warnings \*(Aqthreads\*(Aq; cond_signal($foo); }.Ve.IP "cond_broadcast \s-1VARIABLE\s0" 4.IX Item "cond_broadcast VARIABLE"The \f(CW\*(C`cond_broadcast\*(C'\fR function works similarly to \f(CW\*(C`cond_signal\*(C'\fR.\&\f(CW\*(C`cond_broadcast\*(C'\fR, though, will unblock \fBall\fR the threads that are blocked ina \f(CW\*(C`cond_wait\*(C'\fR on the locked variable, rather than only one..SH "OBJECTS".IX Header "OBJECTS"threads::shared exports a version of \fIbless()\fR thatworks on shared objects such that \fIblessings\fR propagate across threads..PP.Vb 5\& # Create a shared \*(Aqfoo\*(Aq object\& my $foo;\& share($foo);\& $foo = &share({});\& bless($foo, \*(Aqfoo\*(Aq);\&\& # Create a shared \*(Aqbar\*(Aq object\& my $bar;\& share($bar);\& $bar = &share({});\& bless($bar, \*(Aqbar\*(Aq);\&\& # Put \*(Aqbar\*(Aq inside \*(Aqfoo\*(Aq\& $foo\->{\*(Aqbar\*(Aq} = $bar;\&\& # Rebless the objects via a thread\& threads\->create(sub {\& # Rebless the outer object\& bless($foo, \*(Aqyin\*(Aq);\&\& # Cannot directly rebless the inner object\& #bless($foo\->{\*(Aqbar\*(Aq}, \*(Aqyang\*(Aq);\&\& # Retrieve and rebless the inner object\& my $obj = $foo\->{\*(Aqbar\*(Aq};\& bless($obj, \*(Aqyang\*(Aq);\& $foo\->{\*(Aqbar\*(Aq} = $obj;\&\& })\->join();\&\& print(ref($foo), "\en"); # Prints \*(Aqyin\*(Aq\& print(ref($foo\->{\*(Aqbar\*(Aq}), "\en"); # Prints \*(Aqyang\*(Aq\& print(ref($bar), "\en"); # Also prints \*(Aqyang\*(Aq.Ve.SH "NOTES".IX Header "NOTES"threads::shared is designed to disable itself silently if threads are notavailable. If you want access to threads, you must \f(CW\*(C`use threads\*(C'\fR before you\&\f(CW\*(C`use threads::shared\*(C'\fR. threads will emit a warning if you use it afterthreads::shared..SH "BUGS AND LIMITATIONS".IX Header "BUGS AND LIMITATIONS"When \f(CW\*(C`share\*(C'\fR is used on arrays, hashes, array refs or hash refs, any datathey contain will be lost..PP.Vb 3\& my @arr = qw(foo bar baz);\& share(@arr);\& # @arr is now empty (i.e., == ());\&\& # Create a \*(Aqfoo\*(Aq object\& my $foo = { \*(Aqdata\*(Aq => 99 };\& bless($foo, \*(Aqfoo\*(Aq);\&\& # Share the object\& share($foo); # Contents are now wiped out\& print("ERROR: \e$foo is empty\en")\& if (! exists($foo\->{\*(Aqdata\*(Aq}));.Ve.PPTherefore, populate such variables \fBafter\fR declaring them as shared. (Scalarand scalar refs are not affected by this problem.).PPIt is often not wise to share an object unless the class itself has beenwritten to support sharing. For example, an object's destructor may getcalled multiple times, once for each thread's scope exit. Another danger isthat the contents of hash-based objects will be lost due to the abovementioned limitation. See \fIexamples/class.pl\fR (in the \s-1CPAN\s0 distribution ofthis module) for how to create a class that supports object sharing..PPDoes not support \f(CW\*(C`splice\*(C'\fR on arrays!.PPTaking references to the elements of shared arrays and hashes does notautovivify the elements, and neither does slicing a shared array/hash overnon-existent indices/keys autovivify the elements..PP\&\f(CW\*(C`share()\*(C'\fR allows you to \f(CW\*(C`share($hashref\->{key})\*(C'\fR without giving anyerror message. But the \f(CW\*(C`$hashref\->{key}\*(C'\fR is \fBnot\fR shared, causing theerror \*(L"locking can only be used on shared values\*(R" to occur when you attempt to\&\f(CW\*(C`lock($hasref\->{key})\*(C'\fR..PPView existing bug reports at, and submit any new bugs, problems, patches, etc.to: <http://rt.cpan.org/NoAuth/Bugs.html?Dist=threads\-shared>.SH "SEE ALSO".IX Header "SEE ALSO"threads::shared Discussion Forum on \s-1CPAN:\s0<http://www.cpanforum.com/dist/threads\-shared>.PPAnnotated \s-1POD\s0 for threads::shared:<http://annocpan.org/~JDHEDDEN/threads\-shared\-1.14/shared.pm>.PPSource repository:<http://code.google.com/p/threads\-shared/>.PPthreads, perlthrtut.PP<http://www.perl.com/pub/a/2002/06/11/threads.html> and<http://www.perl.com/pub/a/2002/09/04/threads.html>.PPPerl threads mailing list:<http://lists.cpan.org/showlist.cgi?name=iThreads>.SH "AUTHOR".IX Header "AUTHOR"Artur Bergman <sky \s-1AT\s0 crucially \s-1DOT\s0 net>.PPthreads::shared is released under the same license as Perl..PPDocumentation borrowed from the old Thread.pm..PP\&\s-1CPAN\s0 version produced by Jerry D. Hedden <jdhedden \s-1AT\s0 cpan \s-1DOT\s0 org>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -