source: trunk/doc/html/qsemaphore.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 7.0 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/src/tools/qsemaphore.cpp:45 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>QSemaphore Class</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QSemaphore Class Reference</h1>
33
34<p>The QSemaphore class provides a robust integer semaphore.
35<a href="#details">More...</a>
36<p>All the functions in this class are <a href="threads.html#threadsafe">thread-safe</a> when Qt is built with thread support.</p>
37<p><tt>#include &lt;<a href="qsemaphore-h.html">qsemaphore.h</a>&gt;</tt>
38<p><a href="qsemaphore-members.html">List of all member functions.</a>
39<h2>Public Members</h2>
40<ul>
41<li class=fn><a href="#QSemaphore"><b>QSemaphore</b></a> ( int&nbsp;maxcount )</li>
42<li class=fn>virtual <a href="#~QSemaphore"><b>~QSemaphore</b></a> ()</li>
43<li class=fn>int <a href="#available"><b>available</b></a> () const</li>
44<li class=fn>int <a href="#total"><b>total</b></a> () const</li>
45<li class=fn>int <a href="#operator++"><b>operator++</b></a> ( int )</li>
46<li class=fn>int <a href="#operator--"><b>operator--</b></a> ( int )</li>
47<li class=fn>int <a href="#operator+-eq"><b>operator+=</b></a> ( int&nbsp;n )</li>
48<li class=fn>int <a href="#operator--eq"><b>operator-=</b></a> ( int&nbsp;n )</li>
49<li class=fn>bool <a href="#tryAccess"><b>tryAccess</b></a> ( int&nbsp;n )</li>
50</ul>
51<hr><a name="details"></a><h2>Detailed Description</h2>
52
53
54
55The QSemaphore class provides a robust integer semaphore.
56<p>
57
58<p> A QSemaphore can be used to serialize thread execution, in a
59similar way to a <a href="qmutex.html">QMutex</a>. A semaphore differs from a mutex, in
60that a semaphore can be accessed by more than one thread at a
61time.
62<p> For example, suppose we have an application that stores data in a
63large tree structure. The application creates 10 threads
64(commonly called a thread pool) to perform searches on the tree.
65When the application searches the tree for some piece of data, it
66uses one thread per base node to do the searching. A semaphore
67could be used to make sure that two threads don't try to search
68the same branch of the tree at the same time.
69<p> A non-computing example of a semaphore would be dining at a
70restuarant. A semaphore is initialized to have a maximum count
71equal to the number of chairs in the restuarant. As people
72arrive, they want a seat. As seats are filled, the semaphore is
73accessed, once per person. As people leave, the access is
74released, allowing more people to enter. If a party of 10 people
75want to be seated, but there are only 9 seats, those 10 people
76will wait, but a party of 4 people would be seated (taking the
77available seats to 5, making the party of 10 people wait longer).
78<p> When a semaphore is created it is given a number which is the
79maximum number of concurrent accesses it will permit. Accesses to
80the sempahore are gained using <a href="#operator++">operator++</a>() or <a href="#operator+-eq">operator+=</a>(), and
81released with <a href="#operator--">operator--</a>() or <a href="#operator--eq">operator-=</a>(). The number of
82accesses allowed is retrieved with <a href="#available">available</a>(), and the total
83number with <a href="#total">total</a>(). Note that the incrementing functions will
84block if there aren't enough available accesses. Use <a href="#tryAccess">tryAccess</a>()
85if you want to acquire accesses without blocking.
86<p>See also <a href="environment.html">Environment Classes</a> and <a href="thread.html">Threading</a>.
87
88<hr><h2>Member Function Documentation</h2>
89<h3 class=fn><a name="QSemaphore"></a>QSemaphore::QSemaphore ( int&nbsp;maxcount )
90</h3>
91Creates a new semaphore. The semaphore can be concurrently
92accessed at most <em>maxcount</em> times.
93
94<h3 class=fn><a name="~QSemaphore"></a>QSemaphore::~QSemaphore ()<tt> [virtual]</tt>
95</h3>
96Destroys the semaphore.
97<p> <b>Warning:</b> If you destroy a semaphore that has accesses in use the
98resultant behavior is undefined.
99
100<h3 class=fn>int <a name="available"></a>QSemaphore::available () const
101</h3>
102Returns the number of accesses currently available to the
103semaphore.
104
105<h3 class=fn>int <a name="operator++"></a>QSemaphore::operator++ ( int )
106</h3>
107Postfix ++ operator.
108<p> Try to get access to the semaphore. If <a href="#available">available</a>() == 0, this
109call will block until it can get access, i.e. until <a href="#available">available</a>() &gt;
1100.
111
112<h3 class=fn>int <a name="operator+-eq"></a>QSemaphore::operator+= ( int&nbsp;n )
113</h3>
114Try to get access to the semaphore. If <a href="#available">available</a>() &lt; <em>n</em>, this
115call will block until it can get all the accesses it wants, i.e.
116until <a href="#available">available</a>() &gt;= <em>n</em>.
117
118<h3 class=fn>int <a name="operator--"></a>QSemaphore::operator-- ( int )
119</h3>
120Postfix -- operator.
121<p> Release access of the semaphore. This wakes all threads waiting
122for access to the semaphore.
123
124<h3 class=fn>int <a name="operator--eq"></a>QSemaphore::operator-= ( int&nbsp;n )
125</h3>
126Release <em>n</em> accesses to the semaphore.
127
128<h3 class=fn>int <a name="total"></a>QSemaphore::total () const
129</h3>
130Returns the total number of accesses to the semaphore.
131
132<h3 class=fn>bool <a name="tryAccess"></a>QSemaphore::tryAccess ( int&nbsp;n )
133</h3>
134Try to get access to the semaphore. If <a href="#available">available</a>() &lt; <em>n</em>, this
135function will return FALSE immediately. If <a href="#available">available</a>() &gt;= <em>n</em>,
136this function will take <em>n</em> accesses and return TRUE. This
137function does <em>not</em> block.
138
139<!-- eof -->
140<hr><p>
141This file is part of the <a href="index.html">Qt toolkit</a>.
142Copyright &copy; 1995-2007
143<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
144<table width=100% cellspacing=0 border=0><tr>
145<td>Copyright &copy; 2007
146<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
147<td align=right><div align=right>Qt 3.3.8</div>
148</table></div></address></body>
149</html>
Note: See TracBrowser for help on using the repository browser.