1
<?xml version="1.0" encoding="iso-8859-1"?>
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/qt3support/tools/q3semaphore.cpp -->
7
<title>Qt 4.0: Q3Semaphore 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>
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
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"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <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">Q3Semaphore Class Reference</h1>
21
<p>The Q3Semaphore class provides a robust integer semaphore. <a href="#details">More...</a></p>
22
<pre>#include <Q3Semaphore></pre><p><b>This class is part of the Qt 3 support library.</b> It is provided to keep old source code working. We strongly advise against using it in new code. See the <a href="porting4.html">Porting Guide</a> for more information.</p>
23
<p>Part of the <a href="qt3support.html">Qt3Support</a> module.</p>
24
<p><b>Note:</b> All the functions in this class are <a href="threads.html#thread-safe">thread-safe</a>.</p>
26
<li><a href="q3semaphore-members.html">List of all members, including inherited members</a></li>
28
<a name="public-functions"></a>
29
<h3>Public Functions</h3>
31
<li><div class="fn"/><b><a href="q3semaphore.html#Q3Semaphore">Q3Semaphore</a></b> ( int <i>maxcount</i> )</li>
32
<li><div class="fn"/>virtual <b><a href="q3semaphore.html#dtor.Q3Semaphore">~Q3Semaphore</a></b> ()</li>
33
<li><div class="fn"/>int <b><a href="q3semaphore.html#available">available</a></b> () const</li>
34
<li><div class="fn"/>int <b><a href="q3semaphore.html#total">total</a></b> () const</li>
35
<li><div class="fn"/>bool <b><a href="q3semaphore.html#tryAccess">tryAccess</a></b> ( int <i>n</i> )</li>
36
<li><div class="fn"/>int <b><a href="q3semaphore.html#operator-2b-2b">operator++</a></b> ( int )</li>
37
<li><div class="fn"/>int <b><a href="q3semaphore.html#operator-2b-eq">operator+=</a></b> ( int <i>n</i> )</li>
38
<li><div class="fn"/>int <b><a href="q3semaphore.html#operator--">operator--</a></b> ( int )</li>
39
<li><div class="fn"/>int <b><a href="q3semaphore.html#operator--eq">operator-=</a></b> ( int <i>n</i> )</li>
41
<a name="details"></a>
43
<h2>Detailed Description</h2>
44
<p>The Q3Semaphore class provides a robust integer semaphore.</p>
45
<p>A Q3Semaphore can be used to serialize thread execution, in a similar way to a <a href="qmutex.html">QMutex</a>. A semaphore differs from a mutex, in that a semaphore can be accessed by more than one thread at a time.</p>
46
<p>For example, suppose we have an application that stores data in a large tree structure. The application creates 10 threads (commonly called a thread pool) to perform searches on the tree. When the application searches the tree for some piece of data, it uses one thread per base node to do the searching. A semaphore could be used to make sure that two threads don't try to search the same branch of the tree at the same time.</p>
47
<p>A non-computing example of a semaphore would be dining at a restaurant. A semaphore is initialized to have a maximum count equal to the number of chairs in the restaurant. As people arrive, they want a seat. As seats are filled, the semaphore is accessed, once per person. As people leave, the access is released, 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>
48
<p>When a semaphore is created it is given a number which is the maximum number of concurrent accesses it will permit. This amount may be changed using operator++(), operator--(), operator+=() and operator-=(). The number of accesses allowed is retrieved with <a href="q3semaphore.html#available">available</a>(), and the total number with <a href="q3semaphore.html#total">total</a>(). Note that the incrementing functions will block if there aren't enough available accesses. Use <a href="q3semaphore.html#tryAccess">tryAccess</a>() if you want to acquire accesses without blocking.</p>
50
<h2>Member Function Documentation</h2>
51
<h3 class="fn"><a name="Q3Semaphore"></a>Q3Semaphore::Q3Semaphore ( int <i>maxcount</i> )</h3>
52
<p>Creates a new semaphore. The semaphore can be concurrently accessed at most <i>maxcount</i> times.</p>
53
<h3 class="fn"><a name="dtor.Q3Semaphore"></a>Q3Semaphore::~Q3Semaphore () <tt> [virtual]</tt></h3>
54
<p>Destroys the semaphore.</p>
55
<p><b>Warning:</b> If you destroy a semaphore that has accesses in use the resultant behavior is undefined.</p>
56
<h3 class="fn"><a name="available"></a>int Q3Semaphore::available () const</h3>
57
<p>Returns the number of accesses currently available to the semaphore.</p>
58
<h3 class="fn"><a name="total"></a>int Q3Semaphore::total () const</h3>
59
<p>Returns the total number of accesses to the semaphore.</p>
60
<h3 class="fn"><a name="tryAccess"></a>bool Q3Semaphore::tryAccess ( int <i>n</i> )</h3>
61
<p>Try to get access to the semaphore. If <a href="q3semaphore.html#available">available</a>() < <i>n</i>, this function will return false immediately. If <a href="q3semaphore.html#available">available</a>() >= <i>n</i>, this function will take <i>n</i> accesses and return true. This function does <i>not</i> block.</p>
62
<h3 class="fn"><a name="operator-2b-2b"></a>int Q3Semaphore::operator++ ( int )</h3>
63
<p>Postfix ++ operator.</p>
64
<p>Try to get access to the semaphore. If <a href="q3semaphore.html#available">available</a>() == 0, this call will block until it can get access, i.e. until <a href="q3semaphore.html#available">available</a>() > 0.</p>
65
<h3 class="fn"><a name="operator-2b-eq"></a>int Q3Semaphore::operator+= ( int <i>n</i> )</h3>
66
<p>Try to get access to the semaphore. If <a href="q3semaphore.html#available">available</a>() < <i>n</i>, this call will block until it can get all the accesses it wants, i.e. until <a href="q3semaphore.html#available">available</a>() >= <i>n</i>.</p>
67
<h3 class="fn"><a name="operator--"></a>int Q3Semaphore::operator-- ( int )</h3>
68
<p>Postfix -- operator.</p>
69
<p>Release access of the semaphore. This wakes all threads waiting for access to the semaphore.</p>
70
<h3 class="fn"><a name="operator--eq"></a>int Q3Semaphore::operator-= ( int <i>n</i> )</h3>
71
<p>Release <i>n</i> accesses to the semaphore.</p>
72
<p /><address><hr /><div align="center">
73
<table width="100%" cellspacing="0" border="0"><tr class="address">
74
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
75
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
76
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
77
</tr></table></div></address></body>