⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08_210.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Thread (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_209.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_211.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.210. Thread</h2><p><a name="INDEX-1578" />Provides Perlmultithreading support. Distributed as a beta feature with Perl 5.005and runs only on versions of Perl that were built with threadsupport. Has the following functions and methods.</p><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">t</em> = new Thread \<em class="replaceable">&amp;sub</em>[, <em class="replaceable">params</em>]</pre><p>Constructor. Starts a new thread in the referenced subroutine,<em class="replaceable"><tt>\sub</tt></em>, returning a thread object thatrepresents the new thread. The optional parameter list is passed tothe subroutine. Execution continues in both the new thread and thecode.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>async</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">t</em> = async {<em class="replaceable">block</em>};</pre><p>Creates a new thread to execute the block that follows it. The blockis treated as an anonymous subroutine (and therefore has a semicolonafter the closing bracket). Returns a thread object.<tt class="literal">async</tt> isn't exported by default,so you can specify <tt class="literal">use</tt> <tt class="literal">Threadqw(async);</tt> or fully qualify the name(<tt class="literal">Thread::async</tt>).</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>cond_broadcast</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>cond_broadcast <em class="replaceable">var</em></pre><p>Like <tt class="literal">cond_wait</tt>, but unblocks all threads blockedin a <tt class="literal">cond_wait</tt> on the locked variable, not justone thread.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>cond_signal</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>cond_signal <em class="replaceable">var</em></pre><p>Takes the locked variable <em class="replaceable">var</em> and unblocksone thread that's <tt class="literal">cond_wait</tt> ing onthat variable. If multiple threads are so blocked, the one that willbe unblocked can't be determined.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>cond_wait</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>cond_wait <em class="replaceable">var</em></pre><p>Takes the locked variable <em class="replaceable">var</em>, unlocks it,and blocks until another thread does a <tt class="literal">cond_signal</tt>or <tt class="literal">cond_broadcast</tt> for that variable. The variableis relocked after the <tt class="literal">cond_wait</tt> has beensatisfied. If multiple threads are <tt class="literal">cond_wait</tt> ing,all but one will reblock waiting to reacquire the lock.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>eval</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>eval {$<em class="replaceable">t</em>-&gt;join}</pre><p>Wraps an <tt class="literal">eval</tt> around a <tt class="literal">join</tt>.Waits for a thread to exit and passes any return values from thethread, putting errors into <tt class="literal">$@</tt>.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>join</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">t</em>-&gt;join</pre><p>Waits for a thread to end. When it does, returns any exit values fromthe thread. Blocks until the thread has ended, unless the thread hasalready terminated.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>list</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre><em class="replaceable">Thread</em>-&gt;list</pre><p>Returns list of thread objects for all running and finished, but not<tt class="literal">join</tt> ed, threads.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>lock</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>lock <em class="replaceable">var</em>lock <em class="replaceable">sub</em></pre></td><td align="right" /></tr></table><p><p>Locks a variable or a subroutine. A lock on a variable is maintaineduntil the lock goes out of scope. If the variable is already lockedby another thread, the <tt class="literal">lock</tt> call blocks until thevariable is available. You can recursively lock the variable, whichstays locked until the outermost lock goes out of scope.</p><p>Note that locks on variables affect only other<tt class="literal">lock</tt> calls; they don't preventnormal access to a variable. Also, locking a container object (e.g.,an array) doesn't lock each element of thecontainer.</p><p>Locking a subroutine blocks any calls to the subroutine until thelock goes out of scope; no other thread can access the subroutinewhile the lock is in effect.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>self</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre><em class="replaceable">Thread</em>-&gt;self</pre><p>Returns an object representing the thread that made the call.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>tid</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">id</em> = $<em class="replaceable">t</em>-&gt;tid</pre><p>Returns an object representing the <tt class="literal">tid</tt> (thread ID)of a thread. The ID is simply an integer that is incremented eachtime a new thread is created, starting with zero for the main threadof a program.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_209.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_211.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.209. Text::Wrap</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.211. Thread::Queue</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -