File: qsemaphore.html

package info (click to toggle)
python-qt4 4.7.3-1%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,504 kB
  • ctags: 4,680
  • sloc: python: 28,738; cpp: 8,897; sh: 245; xml: 243; makefile: 150
file content (61 lines) | stat: -rw-r--r-- 8,898 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QSemaphore Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="../pyqt4ref.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QSemaphore Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QSemaphore class provides a general counting semaphore. <a href="#details">More...</a></p>
<h3>Methods</h3><ul><li><div class="fn" /><b><a href="qsemaphore.html#QSemaphore">__init__</a></b> (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;0)</li><li><div class="fn" /><b><a href="qsemaphore.html#acquire">acquire</a></b> (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;1)</li><li><div class="fn" />int <b><a href="qsemaphore.html#available">available</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsemaphore.html#release">release</a></b> (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;1)</li><li><div class="fn" />bool <b><a href="qsemaphore.html#tryAcquire">tryAcquire</a></b> (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;1)</li><li><div class="fn" />bool <b><a href="qsemaphore.html#tryAcquire-2">tryAcquire</a></b> (<i>self</i>, int, int)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QSemaphore class provides a general counting semaphore.</p>
<p>A semaphore is a generalization of a mutex. While a mutex can only be locked once, it's possible to acquire a semaphore multiple times. Semaphores are typically used to protect a certain number of identical resources.</p>
<p>Semaphores support two fundamental operations, <a href="qsemaphore.html#acquire">acquire</a>() and <a href="qsemaphore.html#release">release</a>():</p>
<ul>
<li>acquire(<i>n</i>) tries to acquire <i>n</i> resources. If there aren't that many resources available, the call will block until this is the case.</li>
<li>release(<i>n</i>) releases <i>n</i> resources.</li>
</ul>
<p>There's also a <a href="qsemaphore.html#tryAcquire">tryAcquire</a>() function that returns immediately if it cannot acquire the resources, and an <a href="qsemaphore.html#available">available</a>() function that returns the number of available resources at any time.</p>
<p>Example:</p>
<pre> QSemaphore sem(5);      <span class="comment">// sem.available() == 5</span>

 sem.acquire(3);         <span class="comment">// sem.available() == 2</span>
 sem.acquire(2);         <span class="comment">// sem.available() == 0</span>
 sem.release(5);         <span class="comment">// sem.available() == 5</span>
 sem.release(5);         <span class="comment">// sem.available() == 10</span>

 sem.tryAcquire(1);      <span class="comment">// sem.available() == 9, returns true</span>
 sem.tryAcquire(250);    <span class="comment">// sem.available() == 9, returns false</span></pre>
<p>A typical application of semaphores is for controlling access to a circular buffer shared by a producer thread and a consumer thread. The <a href="threads-semaphores.html">Semaphores</a> example shows how to use QSemaphore to solve that problem.</p>
<p>A non-computing example of a semaphore would be dining at a restaurant. A semaphore is initialized with the number of chairs in the restaurant. As people arrive, they want a seat. As seats are filled, <a href="qsemaphore.html#available">available</a>() is decremented. As people leave, the <a href="qsemaphore.html#available">available</a>() is incremented, allowing more people to enter. If a party of 10 people want to be seated, but there are only 9 seats, those 10 people will wait, but a party of 4 people would be seated (taking the available seats to 5, making the party of 10 people wait longer).</p>
<p>See also <a href="qmutex.html">QMutex</a>, <a href="qwaitcondition.html">QWaitCondition</a>, <a href="qthread.html">QThread</a>, and <a href="threads-semaphores.html">Semaphores Example</a>.</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QSemaphore" />QSemaphore.__init__ (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;0)</h3><p>Creates a new semaphore and initializes the number of resources it guards to <i>n</i> (by default, 0).</p>
<p>See also <a href="qsemaphore.html#release">release</a>() and <a href="qsemaphore.html#available">available</a>().</p>
<a name="//apple_ref/cpp/instm/QSemaphore/~QSemaphore" />
<h3 class="fn"><a name="acquire" />QSemaphore.acquire (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;1)</h3><p>Tries to acquire <tt>n</tt> resources guarded by the semaphore. If <i>n</i> &gt; <a href="qsemaphore.html#available">available</a>(), this call will block until enough resources are available.</p>
<p>See also <a href="qsemaphore.html#release">release</a>(), <a href="qsemaphore.html#available">available</a>(), and <a href="qsemaphore.html#tryAcquire">tryAcquire</a>().</p>
<a name="//apple_ref/cpp/instm/QSemaphore/available" />
<h3 class="fn"><a name="available" />int QSemaphore.available (<i>self</i>)</h3><p>Returns the number of resources currently available to the semaphore. This number can never be negative.</p>
<p>See also <a href="qsemaphore.html#acquire">acquire</a>() and <a href="qsemaphore.html#release">release</a>().</p>
<a name="//apple_ref/cpp/instm/QSemaphore/release" />
<h3 class="fn"><a name="release" />QSemaphore.release (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;1)</h3><p>Releases <i>n</i> resources guarded by the semaphore.</p>
<p>This function can be used to "create" resources as well. For example:</p>
<pre> QSemaphore sem(5);      <span class="comment">// a semaphore that guards 5 resources</span>
 sem.acquire(5);         <span class="comment">// acquire all 5 resources</span>
 sem.release(5);         <span class="comment">// release the 5 resources</span>
 sem.release(10);        <span class="comment">// "create" 10 new resources</span></pre>
<p>See also <a href="qsemaphore.html#acquire">acquire</a>() and <a href="qsemaphore.html#available">available</a>().</p>
<a name="//apple_ref/cpp/instm/QSemaphore/tryAcquire" />
<h3 class="fn"><a name="tryAcquire" />bool QSemaphore.tryAcquire (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;1)</h3><p>Tries to acquire <tt>n</tt> resources guarded by the semaphore and returns true on success. If <a href="qsemaphore.html#available">available</a>() &lt; <i>n</i>, this call immediately returns false without acquiring any resources.</p>
<p>Example:</p>
<pre> QSemaphore sem(5);      <span class="comment">// sem.available() == 5</span>
 sem.tryAcquire(250);    <span class="comment">// sem.available() == 5, returns false</span>
 sem.tryAcquire(3);      <span class="comment">// sem.available() == 2, returns true</span></pre>
<p>See also <a href="qsemaphore.html#acquire">acquire</a>().</p>
<h3 class="fn"><a name="tryAcquire-2" />bool QSemaphore.tryAcquire (<i>self</i>, int, int)</h3><p>Tries to acquire <tt>n</tt> resources guarded by the semaphore and returns true on success. If <a href="qsemaphore.html#available">available</a>() &lt; <i>n</i>, this call will wait for at most <i>timeout</i> milliseconds for resources to become available.</p>
<p>Note: Passing a negative number as the <i>timeout</i> is equivalent to calling <a href="qsemaphore.html#acquire">acquire</a>(), i.e. this function will wait forever for resources to become available if <i>timeout</i> is negative.</p>
<p>Example:</p>
<pre> QSemaphore sem(5);            <span class="comment">// sem.available() == 5</span>
 sem.tryAcquire(250, 1000);    <span class="comment">// sem.available() == 5, waits 1000 milliseconds and returns false</span>
 sem.tryAcquire(3, 30000);     <span class="comment">// sem.available() == 2, returns true without waiting</span></pre>
<p>See also <a href="qsemaphore.html#acquire">acquire</a>().</p>
<p /><address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;4.7.3 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2010</td><td align="right" width="25%">Qt&#160;4.6.2</td></tr></table></div></address></body></html>