semaphore.html
来自「perl教程」· HTML 代码 · 共 117 行
HTML
117 行
<?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::Semaphore - thread-safe semaphores</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__',2);</script>
<h1><a>Thread::Semaphore - thread-safe semaphores</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#functions_and_methods">FUNCTIONS AND METHODS</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Thread::Semaphore - thread-safe semaphores</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="keyword">use</span> <span class="variable">Thread::Semaphore</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$s</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">Thread::Semaphore</span><span class="operator">;</span>
<span class="variable">$s</span><span class="operator">-></span><span class="variable">down</span><span class="operator">;</span> <span class="comment"># Also known as the semaphore P operation.</span>
<span class="comment"># The guarded section is here</span>
<span class="variable">$s</span><span class="operator">-></span><span class="variable">up</span><span class="operator">;</span> <span class="comment"># Also known as the semaphore V operation.</span>
</pre>
<pre>
<span class="comment"># The default semaphore value is 1.</span>
<span class="keyword">my</span> <span class="variable">$s</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">Thread::Semaphore</span><span class="operator">(</span><span class="variable">$initial_value</span><span class="operator">);</span>
<span class="variable">$s</span><span class="operator">-></span><span class="variable">down</span><span class="operator">(</span><span class="variable">$down_value</span><span class="operator">);</span>
<span class="variable">$s</span><span class="operator">-></span><span class="variable">up</span><span class="operator">(</span><span class="variable">$up_value</span><span class="operator">);</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Semaphores provide a mechanism to regulate access to resources. Semaphores,
unlike locks, aren't tied to particular scalars, and so may be used to
control access to anything you care to use them for.</p>
<p>Semaphores don't limit their values to zero or one, so they can be used to
control access to some resource that there may be more than one of. (For
example, filehandles.) Increment and decrement amounts aren't fixed at one
either, so threads can reserve or return multiple resources at once.</p>
<p>
</p>
<hr />
<h1><a name="functions_and_methods">FUNCTIONS AND METHODS</a></h1>
<dl>
<dt><strong><a name="item_new">new</a></strong>
<dt><strong>new NUMBER</strong>
<dd>
<p><a href="#item_new"><code>new</code></a> creates a new semaphore, and initializes its count to the passed
number. If no number is passed, the semaphore's count is set to one.</p>
</dd>
</li>
<dt><strong><a name="item_down">down</a></strong>
<dt><strong>down NUMBER</strong>
<dd>
<p>The <a href="#item_down"><code>down</code></a> method decreases the semaphore's count by the specified number,
or by one if no number has been specified. If the semaphore's count would drop
below zero, this method will block until such time that the semaphore's
count is equal to or larger than the amount you're <a href="#item_down"><code>down</code></a>ing the
semaphore's count by.</p>
</dd>
<dd>
<p>This is the semaphore "P operation" (the name derives from the Dutch
word "pak", which means "capture" -- the semaphore operations were
named by the late Dijkstra, who was Dutch).</p>
</dd>
</li>
<dt><strong><a name="item_up">up</a></strong>
<dt><strong>up NUMBER</strong>
<dd>
<p>The <a href="#item_up"><code>up</code></a> method increases the semaphore's count by the number specified,
or by one if no number has been specified. This will unblock any thread blocked
trying to <a href="#item_down"><code>down</code></a> the semaphore if the <a href="#item_up"><code>up</code></a> raises the semaphore count
above the amount that the <a href="#item_down"><code>down</code></a>s are trying to decrement it by.</p>
</dd>
<dd>
<p>This is the semaphore "V operation" (the name derives from the Dutch
word "vrij", which means "release").</p>
</dd>
</li>
</dl>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?