thread.html
来自「perl教程」· HTML 代码 · 共 339 行 · 第 1/2 页
HTML
339 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../displayToc.js"></script>
<script language="JavaScript" src="../tocParas.js"></script>
<script language="JavaScript" src="../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../scineplex.css">
<title>Thread - manipulate threads in Perl</title>
<link rel="stylesheet" href="../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',1);</script>
<h1><a>Thread - manipulate threads in Perl</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#caveat">CAVEAT</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#functions">FUNCTIONS</a></li>
<li><a href="#methods">METHODS</a></li>
<li><a href="#limitations">LIMITATIONS</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Thread - manipulate threads in Perl (for old code only)</p>
<p>
</p>
<hr />
<h1><a name="caveat">CAVEAT</a></h1>
<p>Perl has two thread models.</p>
<p>In Perl 5.005 the thread model was that all data is implicitly shared
and shared access to data has to be explicitly synchronized.
This model is called "5005threads".</p>
<p>In Perl 5.6 a new model was introduced in which all is was thread
local and shared access to data has to be explicitly declared.
This model is called "ithreads", for "interpreter threads".</p>
<p>In Perl 5.6 the ithreads model was not available as a public API,
only as an internal API that was available for extension writers,
and to implement <a href="../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> emulation on Win32 platforms.</p>
<p>In Perl 5.8 the ithreads model became available through the <code>threads</code>
module.</p>
<p>Neither model is configured by default into Perl (except, as mentioned
above, in Win32 ithreads are always available.) You can see your
Perl's threading configuration by running <code>perl -V</code> and looking for
the <em>use...threads</em> variables, or inside script by <code>use Config;</code>
and testing for <code>$Config{use5005threads}</code> and <code>$Config{useithreads}</code>.</p>
<p>For old code and interim backwards compatibility, the Thread module
has been reworked to function as a frontend for both 5005threads and
ithreads.</p>
<p>Note that the compatibility is not complete: because the data sharing
models are directly opposed, anything to do with data sharing has to
be thought differently. With the ithreads you must explicitly <code>share()</code>
variables between the threads.</p>
<p>For new code the use of the <code>Thread</code> module is discouraged and
the direct use of the <code>threads</code> and <code>threads::shared</code> modules
is encouraged instead.</p>
<p>Finally, note that there are many known serious problems with the
5005threads, one of the least of which is that regular expression
match variables like $1 are not threadsafe, that is, they easily get
corrupted by competing threads. Other problems include more insidious
data corruption and mysterious crashes. You are seriously urged to
use ithreads instead.</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="keyword">use</span> <span class="variable">Thread</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">my</span> <span class="variable">$t</span> <span class="operator">=</span> <span class="variable">Thread</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(\&</span><span class="variable">start_sub</span><span class="operator">,</span> <span class="variable">@start_args</span><span class="operator">);</span>
</pre>
<pre>
<span class="variable">$result</span> <span class="operator">=</span> <span class="variable">$t</span><span class="operator">-></span><span class="keyword">join</span><span class="operator">;</span>
<span class="variable">$result</span> <span class="operator">=</span> <span class="variable">$t</span><span class="operator">-></span><span class="keyword">eval</span><span class="operator">;</span>
<span class="variable">$t</span><span class="operator">-></span><span class="variable">detach</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">$t</span><span class="operator">-></span><span class="variable">done</span><span class="operator">)</span> <span class="operator">{</span>
<span class="variable">$t</span><span class="operator">-></span><span class="keyword">join</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="keyword">if</span><span class="operator">(</span><span class="variable">$t</span><span class="operator">-></span><span class="variable">equal</span><span class="operator">(</span><span class="variable">$another_thread</span><span class="operator">))</span> <span class="operator">{</span>
<span class="comment"># ...</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="variable">yield</span><span class="operator">();</span>
</pre>
<pre>
<span class="keyword">my</span> <span class="variable">$tid</span> <span class="operator">=</span> <span class="variable">Thread</span><span class="operator">-></span><span class="variable">self</span><span class="operator">-></span><span class="variable">tid</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">lock</span><span class="operator">(</span><span class="variable">$scalar</span><span class="operator">);</span>
<span class="keyword">lock</span><span class="operator">(</span><span class="variable">@array</span><span class="operator">);</span>
<span class="keyword">lock</span><span class="operator">(</span><span class="variable">%hash</span><span class="operator">);</span>
</pre>
<pre>
<span class="keyword">lock</span><span class="operator">(\&</span><span class="variable">sub</span><span class="operator">);</span> <span class="comment"># not available with ithreads</span>
</pre>
<pre>
<span class="variable">$flags</span> <span class="operator">=</span> <span class="variable">$t</span><span class="operator">-></span><span class="variable">flags</span><span class="operator">;</span> <span class="comment"># not available with ithreads</span>
</pre>
<pre>
<span class="keyword">my</span> <span class="variable">@list</span> <span class="operator">=</span> <span class="variable">Thread</span><span class="operator">-></span><span class="variable">list</span><span class="operator">;</span> <span class="comment"># not available with ithreads</span>
</pre>
<pre>
<span class="keyword">use</span> <span class="variable">Thread</span> <span class="string">'async'</span><span class="operator">;</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The <code>Thread</code> module provides multithreading support for perl.</p>
<p>
</p>
<hr />
<h1><a name="functions">FUNCTIONS</a></h1>
<dl>
<dt><strong><a name="item_new">$thread = Thread-><code>new(\&start_sub)</code></a></strong>
<dt><strong>$thread = Thread->new(\&start_sub, LIST)</strong>
<dd>
<p><a href="#item_new"><code>new</code></a> starts a new thread of execution in the referenced subroutine. The
optional list is passed as parameters to the subroutine. Execution
continues in both the subroutine and the code after the <a href="#item_new"><code>new</code></a> call.</p>
</dd>
<dd>
<p><code>Thread-&gt;new</code> returns a thread object representing the newly created
thread.</p>
</dd>
</li>
<dt><strong><a name="item_lock">lock VARIABLE</a></strong>
<dd>
<p><a href="#item_lock"><code>lock</code></a> places a lock on a variable until the lock goes out of scope.</p>
</dd>
<dd>
<p>If the variable is locked by another thread, the <a href="#item_lock"><code>lock</code></a> call will
block until it's available. <a href="#item_lock"><code>lock</code></a> is recursive, so multiple calls
to <a href="#item_lock"><code>lock</code></a> are safe--the variable will remain locked until the
outermost lock on the variable goes out of scope.</p>
</dd>
<dd>
<p>Locks on variables only affect <a href="#item_lock"><code>lock</code></a> calls--they do <em>not</em> affect normal
access to a variable. (Locks on subs are different, and covered in a bit.)
If you really, <em>really</em> want locks to block access, then go ahead and tie
them to something and manage this yourself. This is done on purpose.
While managing access to variables is a good thing, Perl doesn't force
you out of its living room...</p>
</dd>
<dd>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?