~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/qsemaphore.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/src/corelib/thread/qsemaphore.cpp -->
 
6
<head>
 
7
    <title>Qt 4.0: QSemaphore Class Reference</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">QSemaphore Class Reference</h1>
 
21
<p>The QSemaphore class provides a general counting semaphore. <a href="#details">More...</a></p>
 
22
<pre>#include &lt;QSemaphore&gt;</pre><p>Part of the <a href="qtcore.html">QtCore</a> module.</p>
 
23
<p><b>Note:</b> All the functions in this class are <a href="threads.html#thread-safe">thread-safe</a>.</p>
 
24
<ul>
 
25
<li><a href="qsemaphore-members.html">List of all members, including inherited members</a></li>
 
26
</ul>
 
27
<a name="public-functions"></a>
 
28
<h3>Public Functions</h3>
 
29
<ul>
 
30
<li><div class="fn"/><b><a href="qsemaphore.html#QSemaphore">QSemaphore</a></b> ( int <i>n</i> = 0 )</li>
 
31
<li><div class="fn"/><b><a href="qsemaphore.html#dtor.QSemaphore">~QSemaphore</a></b> ()</li>
 
32
<li><div class="fn"/>void <b><a href="qsemaphore.html#acquire">acquire</a></b> ( int <i>n</i> = 1 )</li>
 
33
<li><div class="fn"/>int <b><a href="qsemaphore.html#available">available</a></b> () const</li>
 
34
<li><div class="fn"/>void <b><a href="qsemaphore.html#release">release</a></b> ( int <i>n</i> = 1 )</li>
 
35
<li><div class="fn"/>bool <b><a href="qsemaphore.html#tryAcquire">tryAcquire</a></b> ( int <i>n</i> = 1 )</li>
 
36
</ul>
 
37
<a name="details"></a>
 
38
<hr />
 
39
<h2>Detailed Description</h2>
 
40
<p>The QSemaphore class provides a general counting semaphore.</p>
 
41
<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>
 
42
<p>Semaphores support two fundamental operations, <a href="qsemaphore.html#acquire">acquire</a>() and <a href="qsemaphore.html#release">release</a>():</p>
 
43
<ul>
 
44
<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>
 
45
<li>release(<i>n</i>) releases <i>n</i> resources.</li>
 
46
</ul>
 
47
<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>
 
48
<p>Example:</p>
 
49
<pre>&nbsp;   QSemaphore sem(5);      // sem.available() == 5
 
50
 
 
51
    sem.acquire(3);         // sem.available() == 2
 
52
    sem.acquire(2);         // sem.available() == 0
 
53
    sem.release(5);         // sem.available() == 5
 
54
    sem.release(5);         // sem.available() == 10
 
55
 
 
56
    sem.tryAcquire(1);      // sem.available() == 9, returns true
 
57
    sem.tryAcquire(250);    // sem.available() == 9, returns false</pre>
 
58
<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>
 
59
<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>
 
60
<p>See also <a href="qmutex.html">QMutex</a>, <a href="qwaitcondition.html">QWaitCondition</a>, and <a href="qthread.html">QThread</a>.</p>
 
61
<hr />
 
62
<h2>Member Function Documentation</h2>
 
63
<h3 class="fn"><a name="QSemaphore"></a>QSemaphore::QSemaphore ( int <i>n</i> = 0 )</h3>
 
64
<p>Creates a new semaphore and initializes the number of resources it guards to <i>n</i> (by default, 0).</p>
 
65
<p>See also <a href="qsemaphore.html#release">release</a>() and <a href="qsemaphore.html#available">available</a>().</p>
 
66
<h3 class="fn"><a name="dtor.QSemaphore"></a>QSemaphore::~QSemaphore ()</h3>
 
67
<p>Destroys the semaphore.</p>
 
68
<p><b>Warning:</b> Destroying a semaphore that is in use may result in undefined behavior.</p>
 
69
<h3 class="fn"><a name="acquire"></a>void QSemaphore::acquire ( int <i>n</i> = 1 )</h3>
 
70
<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>
 
71
<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>
 
72
<h3 class="fn"><a name="available"></a>int QSemaphore::available () const</h3>
 
73
<p>Returns the number of resources currently available to the semaphore. This number can never be negative.</p>
 
74
<p>See also <a href="qsemaphore.html#acquire">acquire</a>() and <a href="qsemaphore.html#release">release</a>().</p>
 
75
<h3 class="fn"><a name="release"></a>void QSemaphore::release ( int <i>n</i> = 1 )</h3>
 
76
<p>Releases <i>n</i> resources guarded by the semaphore.</p>
 
77
<p>This function can be used to &quot;create&quot; resources as well. For example:</p>
 
78
<pre>&nbsp;   QSemaphore sem(5);      // a semaphore that guards 5 resources
 
79
    sem.acquire(5);         // acquire all 5 resources
 
80
    sem.release(5);         // release the 5 resources
 
81
    sem.release(10);        // &quot;create&quot; 10 new resources</pre>
 
82
<p>See also <a href="qsemaphore.html#acquire">acquire</a>() and <a href="qsemaphore.html#available">available</a>().</p>
 
83
<h3 class="fn"><a name="tryAcquire"></a>bool QSemaphore::tryAcquire ( int <i>n</i> = 1 )</h3>
 
84
<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>
 
85
<p>Example:</p>
 
86
<pre>&nbsp;   QSemaphore sem(5);      // sem.available() == 5
 
87
    sem.tryAcquire(250);    // sem.available() == 5, returns false
 
88
    sem.tryAcquire(3);      // sem.available() == 2, returns true</pre>
 
89
<p>See also <a href="qsemaphore.html#acquire">acquire</a>().</p>
 
90
<p /><address><hr /><div align="center">
 
91
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
92
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
93
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
94
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
95
</tr></table></div></address></body>
 
96
</html>