[190] | 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"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { 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 Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main 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 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 <<a href="qsemaphore-h.html">qsemaphore.h</a>></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 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 n )</li>
|
---|
| 48 | <li class=fn>int <a href="#operator--eq"><b>operator-=</b></a> ( int n )</li>
|
---|
| 49 | <li class=fn>bool <a href="#tryAccess"><b>tryAccess</b></a> ( int n )</li>
|
---|
| 50 | </ul>
|
---|
| 51 | <hr><a name="details"></a><h2>Detailed Description</h2>
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | The QSemaphore class provides a robust integer semaphore.
|
---|
| 56 | <p>
|
---|
| 57 |
|
---|
| 58 | <p> A QSemaphore can be used to serialize thread execution, in a
|
---|
| 59 | similar way to a <a href="qmutex.html">QMutex</a>. A semaphore differs from a mutex, in
|
---|
| 60 | that a semaphore can be accessed by more than one thread at a
|
---|
| 61 | time.
|
---|
| 62 | <p> For example, suppose we have an application that stores data in a
|
---|
| 63 | large tree structure. The application creates 10 threads
|
---|
| 64 | (commonly called a thread pool) to perform searches on the tree.
|
---|
| 65 | When the application searches the tree for some piece of data, it
|
---|
| 66 | uses one thread per base node to do the searching. A semaphore
|
---|
| 67 | could be used to make sure that two threads don't try to search
|
---|
| 68 | the same branch of the tree at the same time.
|
---|
| 69 | <p> A non-computing example of a semaphore would be dining at a
|
---|
| 70 | restuarant. A semaphore is initialized to have a maximum count
|
---|
| 71 | equal to the number of chairs in the restuarant. As people
|
---|
| 72 | arrive, they want a seat. As seats are filled, the semaphore is
|
---|
| 73 | accessed, once per person. As people leave, the access is
|
---|
| 74 | released, allowing more people to enter. If a party of 10 people
|
---|
| 75 | want to be seated, but there are only 9 seats, those 10 people
|
---|
| 76 | will wait, but a party of 4 people would be seated (taking the
|
---|
| 77 | available 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
|
---|
| 79 | maximum number of concurrent accesses it will permit. Accesses to
|
---|
| 80 | the sempahore are gained using <a href="#operator++">operator++</a>() or <a href="#operator+-eq">operator+=</a>(), and
|
---|
| 81 | released with <a href="#operator--">operator--</a>() or <a href="#operator--eq">operator-=</a>(). The number of
|
---|
| 82 | accesses allowed is retrieved with <a href="#available">available</a>(), and the total
|
---|
| 83 | number with <a href="#total">total</a>(). Note that the incrementing functions will
|
---|
| 84 | block if there aren't enough available accesses. Use <a href="#tryAccess">tryAccess</a>()
|
---|
| 85 | if 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 maxcount )
|
---|
| 90 | </h3>
|
---|
| 91 | Creates a new semaphore. The semaphore can be concurrently
|
---|
| 92 | accessed at most <em>maxcount</em> times.
|
---|
| 93 |
|
---|
| 94 | <h3 class=fn><a name="~QSemaphore"></a>QSemaphore::~QSemaphore ()<tt> [virtual]</tt>
|
---|
| 95 | </h3>
|
---|
| 96 | Destroys the semaphore.
|
---|
| 97 | <p> <b>Warning:</b> If you destroy a semaphore that has accesses in use the
|
---|
| 98 | resultant behavior is undefined.
|
---|
| 99 |
|
---|
| 100 | <h3 class=fn>int <a name="available"></a>QSemaphore::available () const
|
---|
| 101 | </h3>
|
---|
| 102 | Returns the number of accesses currently available to the
|
---|
| 103 | semaphore.
|
---|
| 104 |
|
---|
| 105 | <h3 class=fn>int <a name="operator++"></a>QSemaphore::operator++ ( int )
|
---|
| 106 | </h3>
|
---|
| 107 | Postfix ++ operator.
|
---|
| 108 | <p> Try to get access to the semaphore. If <a href="#available">available</a>() == 0, this
|
---|
| 109 | call will block until it can get access, i.e. until <a href="#available">available</a>() >
|
---|
| 110 | 0.
|
---|
| 111 |
|
---|
| 112 | <h3 class=fn>int <a name="operator+-eq"></a>QSemaphore::operator+= ( int n )
|
---|
| 113 | </h3>
|
---|
| 114 | Try to get access to the semaphore. If <a href="#available">available</a>() < <em>n</em>, this
|
---|
| 115 | call will block until it can get all the accesses it wants, i.e.
|
---|
| 116 | until <a href="#available">available</a>() >= <em>n</em>.
|
---|
| 117 |
|
---|
| 118 | <h3 class=fn>int <a name="operator--"></a>QSemaphore::operator-- ( int )
|
---|
| 119 | </h3>
|
---|
| 120 | Postfix -- operator.
|
---|
| 121 | <p> Release access of the semaphore. This wakes all threads waiting
|
---|
| 122 | for access to the semaphore.
|
---|
| 123 |
|
---|
| 124 | <h3 class=fn>int <a name="operator--eq"></a>QSemaphore::operator-= ( int n )
|
---|
| 125 | </h3>
|
---|
| 126 | Release <em>n</em> accesses to the semaphore.
|
---|
| 127 |
|
---|
| 128 | <h3 class=fn>int <a name="total"></a>QSemaphore::total () const
|
---|
| 129 | </h3>
|
---|
| 130 | Returns the total number of accesses to the semaphore.
|
---|
| 131 |
|
---|
| 132 | <h3 class=fn>bool <a name="tryAccess"></a>QSemaphore::tryAccess ( int n )
|
---|
| 133 | </h3>
|
---|
| 134 | Try to get access to the semaphore. If <a href="#available">available</a>() < <em>n</em>, this
|
---|
| 135 | function will return FALSE immediately. If <a href="#available">available</a>() >= <em>n</em>,
|
---|
| 136 | this function will take <em>n</em> accesses and return TRUE. This
|
---|
| 137 | function does <em>not</em> block.
|
---|
| 138 |
|
---|
| 139 | <!-- eof -->
|
---|
| 140 | <hr><p>
|
---|
| 141 | This file is part of the <a href="index.html">Qt toolkit</a>.
|
---|
| 142 | Copyright © 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 © 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>
|
---|