perlthrtut.html

来自「perl教程」· HTML 代码 · 共 842 行 · 第 1/5 页

HTML
842 行
字号
    <span class="keyword">my</span> <span class="variable">$b</span> <span class="operator">:</span> <span class="variable">shared</span><span class="operator">;</span>
    <span class="keyword">my</span> <span class="variable">$c</span> <span class="operator">:</span> <span class="variable">shared</span><span class="operator">;</span>
    <span class="keyword">my</span> <span class="variable">$thr1</span> <span class="operator">=</span> <span class="variable">threads</span><span class="operator">-&gt;</span><span class="variable">create</span><span class="operator">(</span><span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="variable">$b</span> <span class="operator">=</span> <span class="variable">$a</span><span class="operator">;</span> <span class="variable">$a</span> <span class="operator">=</span> <span class="variable">$b</span> <span class="operator">+</span> <span class="number">1</span><span class="operator">;</span> <span class="operator">});</span> 
    <span class="keyword">my</span> <span class="variable">$thr2</span> <span class="operator">=</span> <span class="variable">threads</span><span class="operator">-&gt;</span><span class="variable">create</span><span class="operator">(</span><span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="variable">$c</span> <span class="operator">=</span> <span class="variable">$a</span><span class="operator">;</span> <span class="variable">$a</span> <span class="operator">=</span> <span class="variable">$c</span> <span class="operator">+</span> <span class="number">1</span><span class="operator">;</span> <span class="operator">});</span>
    <span class="variable">$thr1</span><span class="operator">-&gt;</span><span class="keyword">join</span><span class="operator">;</span>
    <span class="variable">$thr2</span><span class="operator">-&gt;</span><span class="keyword">join</span><span class="operator">;</span>
</pre>
<p>Two threads both access $a.  Each thread can potentially be interrupted
at any point, or be executed in any order.  At the end, $a could be 3
or 4, and both $b and $c could be 2 or 3.</p>
<p>Even <a href="../../lib/Pod/perlvar.html#item__a"><code>$a += 5</code></a> or <code>$a++</code> are not guaranteed to be atomic.</p>
<p>Whenever your program accesses data or resources that can be accessed
by other threads, you must take steps to coordinate access or risk
data inconsistency and race conditions. Note that Perl will protect its
internals from your race conditions, but it won't protect you from you.</p>
<p>
</p>
<hr />
<h1><a name="synchronization_and_control">Synchronization and control</a></h1>
<p>Perl provides a number of mechanisms to coordinate the interactions
between themselves and their data, to avoid race conditions and the like.
Some of these are designed to resemble the common techniques used in thread
libraries such as <code>pthreads</code>; others are Perl-specific. Often, the
standard techniques are clumsy and difficult to get right (such as
condition waits). Where possible, it is usually easier to use Perlish
techniques such as queues, which remove some of the hard work involved.</p>
<p>
</p>
<h2><a name="controlling_access__lock__">Controlling access: <a href="../../lib/Pod/perlfunc.html#item_lock"><code>lock()</code></a></a></h2>
<p>The <a href="../../lib/Pod/perlfunc.html#item_lock"><code>lock()</code></a> function takes a shared variable and puts a lock on it.  
No other thread may lock the variable until the variable is unlocked
by the thread holding the lock. Unlocking happens automatically
when the locking thread exits the outermost block that contains
<a href="../../lib/Pod/perlfunc.html#item_lock"><code>lock()</code></a> function.  Using <a href="../../lib/Pod/perlfunc.html#item_lock"><code>lock()</code></a> is straightforward: this example has
several threads doing some calculations in parallel, and occasionally
updating a running total:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">threads</span><span class="operator">;</span>
    <span class="keyword">use</span> <span class="variable">threads::shared</span><span class="operator">;</span>
</pre>
<pre>
    <span class="keyword">my</span> <span class="variable">$total</span> <span class="operator">:</span> <span class="variable">shared</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
</pre>
<pre>
    <span class="keyword">sub</span><span class="variable"> calc </span><span class="operator">{</span>
        <span class="keyword">for</span> <span class="operator">(;;)</span> <span class="operator">{</span>
            <span class="keyword">my</span> <span class="variable">$result</span><span class="operator">;</span>
            <span class="comment"># (... do some calculations and set $result ...)</span>
            <span class="operator">{</span>
                <span class="keyword">lock</span><span class="operator">(</span><span class="variable">$total</span><span class="operator">);</span> <span class="comment"># block until we obtain the lock</span>
                <span class="variable">$total</span> <span class="operator">+=</span> <span class="variable">$result</span><span class="operator">;</span>
            <span class="operator">}</span> <span class="comment"># lock implicitly released at end of scope</span>
            <span class="keyword">last</span> <span class="keyword">if</span> <span class="variable">$result</span> <span class="operator">==</span> <span class="number">0</span><span class="operator">;</span>
        <span class="operator">}</span>
    <span class="operator">}</span>
</pre>
<pre>
    <span class="keyword">my</span> <span class="variable">$thr1</span> <span class="operator">=</span> <span class="variable">threads</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(\&amp;</span><span class="variable">calc</span><span class="operator">);</span>
    <span class="keyword">my</span> <span class="variable">$thr2</span> <span class="operator">=</span> <span class="variable">threads</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(\&amp;</span><span class="variable">calc</span><span class="operator">);</span>
    <span class="keyword">my</span> <span class="variable">$thr3</span> <span class="operator">=</span> <span class="variable">threads</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(\&amp;</span><span class="variable">calc</span><span class="operator">);</span>
    <span class="variable">$thr1</span><span class="operator">-&gt;</span><span class="keyword">join</span><span class="operator">;</span>
    <span class="variable">$thr2</span><span class="operator">-&gt;</span><span class="keyword">join</span><span class="operator">;</span>
    <span class="variable">$thr3</span><span class="operator">-&gt;</span><span class="keyword">join</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="string">"total=$total\n"</span><span class="operator">;</span>
</pre>
<p><a href="../../lib/Pod/perlfunc.html#item_lock"><code>lock()</code></a> blocks the thread until the variable being locked is
available.  When <a href="../../lib/Pod/perlfunc.html#item_lock"><code>lock()</code></a> returns, your thread can be sure that no other
thread can lock that variable until the outermost block containing the
lock exits.</p>
<p>It's important to note that locks don't prevent access to the variable
in question, only lock attempts.  This is in keeping with Perl's
longstanding tradition of courteous programming, and the advisory file
locking that <a href="../../lib/Pod/perlfunc.html#item_flock"><code>flock()</code></a> gives you.</p>
<p>You may lock arrays and hashes as well as scalars.  Locking an array,
though, will not block subsequent locks on array elements, just lock
attempts on the array itself.</p>
<p>Locks are recursive, which means it's okay for a thread to
lock a variable more than once.  The lock will last until the outermost
<a href="../../lib/Pod/perlfunc.html#item_lock"><code>lock()</code></a> on the variable goes out of scope. For example:</p>
<pre>
    <span class="keyword">my</span> <span class="variable">$x</span> <span class="operator">:</span> <span class="variable">shared</span><span class="operator">;</span>
    <span class="variable">doit</span><span class="operator">();</span>
</pre>
<pre>
    <span class="keyword">sub</span><span class="variable"> doit </span><span class="operator">{</span>
        <span class="operator">{</span>
            <span class="operator">{</span>
                <span class="keyword">lock</span><span class="operator">(</span><span class="variable">$x</span><span class="operator">);</span> <span class="comment"># wait for lock</span>
                <span class="keyword">lock</span><span class="operator">(</span><span class="variable">$x</span><span class="operator">);</span> <span class="comment"># NOOP - we already have the lock</span>
                <span class="operator">{</span>
                    <span class="keyword">lock</span><span class="operator">(</span><span class="variable">$x</span><span class="operator">);</span> <span class="comment"># NOOP</span>
                    <span class="operator">{</span>
                        <span class="keyword">lock</span><span class="operator">(</span><span class="variable">$x</span><span class="operator">);</span> <span class="comment"># NOOP</span>
                        <span class="variable">lockit_some_more</span><span class="operator">();</span>
                    <span class="operator">}</span>
                <span class="operator">}</span>
            <span class="operator">}</span> <span class="comment"># *** implicit unlock here ***</span>
        <span class="operator">}</span>
    <span class="operator">}</span>
</pre>
<pre>
    <span class="keyword">sub</span><span class="variable"> lockit_some_more </span><span class="operator">{</span>
        <span class="keyword">lock</span><span class="operator">(</span><span class="variable">$x</span><span class="operator">);</span> <span class="comment"># NOOP</span>
    <span class="operator">}</span> <span class="comment"># nothing happens here</span>
</pre>
<p>Note that there is no <code>unlock()</code> function - the only way to unlock a
variable is to allow it to go out of scope.</p>
<p>A lock can either be used to guard the data contained within the variable
being locked, or it can be used to guard something else, like a section
of code. In this latter case, the variable in question does not hold any
useful data, and exists only for the purpose of being locked. In this
respect, the variable behaves like the mutexes and basic semaphores of
traditional thread libraries.</p>
<p>
</p>
<h2><a name="a_thread_pitfall__deadlocks">A Thread Pitfall: Deadlocks</a></h2>
<p>Locks are a handy tool to synchronize access to data, and using them
properly is the key to safe shared data.  Unfortunately, locks aren't
without their dangers, especially when multiple locks are involved.
Consider the following code:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">threads</span><span class="operator">;</span>
</pre>
<pre>
    <span class="keyword">my</span> <span class="variable">$a</span> <span class="operator">:</span> <span class="variable">shared</span> <span class="operator">=</span> <span class="number">4</span><span class="operator">;</span> 
    <span class="keyword">my</span> <span class="variable">$b</span> <span class="operator">:</span> <span class="variable">shared</span> <span class="operator">=</span> <span class="string">"foo"</span><span class="operator">;</span> 
    <span class="keyword">my</span> <span class="variable">$thr1</span> <span class="operator">=</span> <span class="variable">threads</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> 
        <span class="keyword">lock</span><span class="operator">(</span><span class="variable">$a</span><span class="operator">);</span> 
        <span class="keyword">sleep</span> <span class="number">20</span><span class="operator">;</span> 
        <span class="keyword">lock</span><span class="operator">(</span><span class="variable">$b</span><span class="operator">);</span> 
    <span class="operator">});</span> 
    <span class="keyword">my</span> <span class="variable">$thr2</span> <span class="operator">=</span> <span class="variable">threads</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> 
        <span class="keyword">lock</span><span class="operator">(</span><span class="variable">$b</span><span class="operator">);</span> 
        <span class="keyword">sleep</span> <span class="number">20</span><span class="operator">;</span> 
        <span class="keyword">lock</span><span class="operator">(</span><span class="variable">$a</span><span class="operator">);</span> 
    <span class="operator">});</span>
</pre>
<p>This program will probably hang until you kill it.  The only way it
won't hang is if one of the two threads acquires both locks
first.  A guaranteed-to-hang version is more complicated, but the
principle is the same.</p>
<p>The first thread will grab a lock on $a, then, after a pause during which
the second thread has probably had time to do some work, try to grab a
lock on $b.  Meanwhile, the second thread grabs a lock on $b, then later
tries to grab a lock on $a.  The second lock attempt for both threads will
block, each waiting for the other to release its lock.</p>
<p>This condition is called a deadlock, and it occurs whenever two or
more threads are trying to get locks on resources that the others
own.  Each thread will block, waiting for the other to release a lock
on a resource.  That never happens, though, since the thread with the
resource is itself waiting for a lock to be released.</p>
<p>There are a number of ways to handle this sort of problem.  The best
way is to always have all threads acquire locks in the exact same
order.  If, for example, you lock variables $a, $b, and $c, always lock
$a before $b, and $b before $c.  It's also best to hold on to locks for
as short a period of time to minimize the risks of deadlock.</p>
<p>The other synchronization primitives described below can suffer from
similar problems.</p>
<p>
</p>
<h2><a name="queues__passing_data_around">Queues: Passing Data Around</a></h2>
<p>A queue is a special thread-safe object that lets you put data in one
end and take it out the other without having to worry about
synchronization issues.  They're pretty straightforward, and look like
this:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">threads</span><span class="operator">;</span> 
    <span class="keyword">use</span> <span class="variable">Thread::Queue</span><span class="operator">;</span>
</pre>

⌨️ 快捷键说明

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