thread.html
来自「perl教程」· HTML 代码 · 共 339 行 · 第 1/2 页
HTML
339 行
<p>If a container object, such as a hash or array, is locked, all the
elements of that container are not locked. For example, if a thread
does a <code>lock @a</code>, any other thread doing a <a href="#item_lock"><code>lock($a[12])</code></a> won't
block.</p>
</dd>
<dd>
<p>With 5005threads you may also <a href="#item_lock"><code>lock</code></a> a sub, using <code>lock &sub</code>.
Any calls to that sub from another thread will block until the lock
is released. This behaviour is not equivalent to declaring the sub
with the <code>locked</code> attribute. The <code>locked</code> attribute serializes
access to a subroutine, but allows different threads non-simultaneous
access. <code>lock &sub</code>, on the other hand, will not allow <em>any</em> other
thread access for the duration of the lock.</p>
</dd>
<dd>
<p>Finally, <a href="#item_lock"><code>lock</code></a> will traverse up references exactly <em>one</em> level.
<a href="#item_lock"><code>lock(\$a)</code></a> is equivalent to <a href="#item_lock"><code>lock($a)</code></a>, while <a href="#item_lock"><code>lock(\\$a)</code></a> is not.</p>
</dd>
</li>
<dt><strong><a name="item_async_block_3b">async BLOCK;</a></strong>
<dd>
<p><code>async</code> creates a thread to execute the block immediately following
it. This block is treated as an anonymous sub, and so must have a
semi-colon after the closing brace. Like <code>Thread-&gt;new</code>, <code>async</code>
returns a thread object.</p>
</dd>
</li>
<dt><strong><a name="item_self">Thread->self</a></strong>
<dd>
<p>The <a href="#item_self"><code>Thread->self</code></a> function returns a thread object that represents
the thread making the <a href="#item_self"><code>Thread->self</code></a> call.</p>
</dd>
</li>
<dt><strong><a name="item_cond_wait">cond_wait VARIABLE</a></strong>
<dd>
<p>The <a href="#item_cond_wait"><code>cond_wait</code></a> function takes a <strong>locked</strong> variable as
a parameter, unlocks the variable, and blocks until another thread
does a <a href="#item_cond_signal"><code>cond_signal</code></a> or <a href="#item_cond_broadcast"><code>cond_broadcast</code></a> for that same locked
variable. The variable that <a href="#item_cond_wait"><code>cond_wait</code></a> blocked on is relocked
after the <a href="#item_cond_wait"><code>cond_wait</code></a> is satisfied. If there are multiple threads
<a href="#item_cond_wait"><code>cond_wait</code></a>ing on the same variable, all but one will reblock waiting
to reaquire the lock on the variable. (So if you're only using
<a href="#item_cond_wait"><code>cond_wait</code></a> for synchronization, give up the lock as soon as
possible.)</p>
</dd>
</li>
<dt><strong><a name="item_cond_signal">cond_signal VARIABLE</a></strong>
<dd>
<p>The <a href="#item_cond_signal"><code>cond_signal</code></a> function takes a locked variable as a parameter and
unblocks one thread that's <a href="#item_cond_wait"><code>cond_wait</code></a>ing on that variable. If more than
one thread is blocked in a <a href="#item_cond_wait"><code>cond_wait</code></a> on that variable, only one (and
which one is indeterminate) will be unblocked.</p>
</dd>
<dd>
<p>If there are no threads blocked in a <a href="#item_cond_wait"><code>cond_wait</code></a> on the variable,
the signal is discarded.</p>
</dd>
</li>
<dt><strong><a name="item_cond_broadcast">cond_broadcast VARIABLE</a></strong>
<dd>
<p>The <a href="#item_cond_broadcast"><code>cond_broadcast</code></a> function works similarly to <a href="#item_cond_signal"><code>cond_signal</code></a>.
<a href="#item_cond_broadcast"><code>cond_broadcast</code></a>, though, will unblock <strong>all</strong> the threads that are
blocked in a <a href="#item_cond_wait"><code>cond_wait</code></a> on the locked variable, rather than only
one.</p>
</dd>
</li>
<dt><strong><a name="item_yield">yield</a></strong>
<dd>
<p>The <a href="#item_yield"><code>yield</code></a> function allows another thread to take control of the
CPU. The exact results are implementation-dependent.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="methods">METHODS</a></h1>
<dl>
<dt><strong><a name="item_join">join</a></strong>
<dd>
<p><a href="#item_join"><code>join</code></a> waits for a thread to end and returns any values the thread
exited with. <a href="#item_join"><code>join</code></a> will block until the thread has ended, though
it won't block if the thread has already terminated.</p>
</dd>
<dd>
<p>If the thread being <a href="#item_join"><code>join</code></a>ed <a href="../lib/Pod/perlfunc.html#item_die"><code>die</code></a>d, the error it died with will
be returned at this time. If you don't want the thread performing
the <a href="#item_join"><code>join</code></a> to die as well, you should either wrap the <a href="#item_join"><code>join</code></a> in
an <a href="#item_eval"><code>eval</code></a> or use the <a href="#item_eval"><code>eval</code></a> thread method instead of <a href="#item_join"><code>join</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_eval">eval</a></strong>
<dd>
<p>The <a href="#item_eval"><code>eval</code></a> method wraps an <a href="#item_eval"><code>eval</code></a> around a <a href="#item_join"><code>join</code></a>, and so waits for
a thread to exit, passing along any values the thread might have returned.
Errors, of course, get placed into <a href="../lib/Pod/perlvar.html#item___"><code>$@</code></a>. (Not available with ithreads.)</p>
</dd>
</li>
<dt><strong><a name="item_detach">detach</a></strong>
<dd>
<p><a href="#item_detach"><code>detach</code></a> 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 want
to catch them, you should use $SIG{__DIE__} or something like that.</p>
</dd>
</li>
<dt><strong><a name="item_equal">equal</a></strong>
<dd>
<p><a href="#item_equal"><code>equal</code></a> tests whether two thread objects represent the same thread and
returns true if they do.</p>
</dd>
</li>
<dt><strong><a name="item_tid">tid</a></strong>
<dd>
<p>The <a href="#item_tid"><code>tid</code></a> method returns the tid of a thread. The tid is
a monotonically increasing integer assigned when a thread is
created. The main thread of a program will have a tid of zero,
while subsequent threads will have tids assigned starting with one.</p>
</dd>
</li>
<dt><strong><a name="item_flags">flags</a></strong>
<dd>
<p>The <a href="#item_flags"><code>flags</code></a> method returns the flags for the thread. This is the
integer value corresponding to the internal flags for the thread,
and the value may not be all that meaningful to you.
(Not available with ithreads.)</p>
</dd>
</li>
<dt><strong><a name="item_done">done</a></strong>
<dd>
<p>The <a href="#item_done"><code>done</code></a> method returns true if the thread you're checking has
finished, and false otherwise. (Not available with ithreads.)</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="limitations">LIMITATIONS</a></h1>
<p>The sequence number used to assign tids is a simple integer, and no
checking is done to make sure the tid isn't currently in use. If a
program creates more than 2**32 - 1 threads in a single run, threads
may be assigned duplicate tids. This limitation may be lifted in
a future version of Perl.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../lib/threads/shared.html">the threads::shared manpage</a> (not available with 5005threads)</p>
<p><a href="../lib/attributes.html">the attributes manpage</a>, <a href="../lib/Thread/Queue.html">the Thread::Queue manpage</a>, <a href="../lib/Thread/Semaphore.html">the Thread::Semaphore manpage</a>,
<a href="../lib/Thread/Specific.html">the Thread::Specific manpage</a> (not available with ithreads)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?