<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- q3semaphore.cpp --> <title>Qt 4.8: Q3Semaphore Class Reference</title> <link rel="stylesheet" type="text/css" href="style/style.css" /> <script src="scripts/jquery.js" type="text/javascript"></script> <script src="scripts/functions.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="style/superfish.css" /> <link rel="stylesheet" type="text/css" href="style/narrow.css" /> <!--[if IE]> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="no"> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie6.css"> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie7.css"> <![endif]--> <!--[if IE 8]> <link rel="stylesheet" type="text/css" href="style/style_ie8.css"> <![endif]--> <script src="scripts/superfish.js" type="text/javascript"></script> <script src="scripts/narrow.js" type="text/javascript"></script> </head> <body class="" onload="CheckEmptyAndLoadList();"> <div class="header" id="qtdocheader"> <div class="content"> <div id="nav-logo"> <a href="index.html">Home</a></div> <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a> <div id="narrowsearch"></div> <div id="nav-topright"> <ul> <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li> <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li> <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/"> DOC</a></li> <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li> </ul> </div> <div id="shortCut"> <ul> <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li> <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li> </ul> </div> <ul class="sf-menu" id="narrowmenu"> <li><a href="#">API Lookup</a> <ul> <li><a href="classes.html">Class index</a></li> <li><a href="functions.html">Function index</a></li> <li><a href="modules.html">Modules</a></li> <li><a href="namespaces.html">Namespaces</a></li> <li><a href="qtglobal.html">Global Declarations</a></li> <li><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </li> <li><a href="#">Qt Topics</a> <ul> <li><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li><a href="supported-platforms.html">Supported Platforms</a></li> <li><a href="technology-apis.html">Qt and Key Technologies</a></li> <li><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </li> <li><a href="#">Examples</a> <ul> <li><a href="all-examples.html">Examples</a></li> <li><a href="tutorials.html">Tutorials</a></li> <li><a href="demos.html">Demos</a></li> <li><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </li> </ul> </div> </div> <div class="wrapper"> <div class="hd"> <span></span> </div> <div class="bd group"> <div class="sidebar"> <div class="searchlabel"> Search index:</div> <div class="search" id="sidebarsearch"> <form id="qtdocsearch" action="" onsubmit="return false;"> <fieldset> <input type="text" name="searchstring" id="pageType" value="" /> <div id="resultdialog"> <a href="#" id="resultclose">Close</a> <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span> results:</p> <ul id="resultlist" class="all"> </ul> </div> </fieldset> </form> </div> <div class="box first bottombar" id="lookup"> <h2 title="API Lookup"><span></span> API Lookup</h2> <div id="list001" class="list"> <ul id="ul001" > <li class="defaultLink"><a href="classes.html">Class index</a></li> <li class="defaultLink"><a href="functions.html">Function index</a></li> <li class="defaultLink"><a href="modules.html">Modules</a></li> <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li> <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li> <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </div> </div> <div class="box bottombar" id="topics"> <h2 title="Qt Topics"><span></span> Qt Topics</h2> <div id="list002" class="list"> <ul id="ul002" > <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li class="defaultLink"><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li> <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </div> </div> <div class="box" id="examples"> <h2 title="Examples"><span></span> Examples</h2> <div id="list003" class="list"> <ul id="ul003"> <li class="defaultLink"><a href="all-examples.html">Examples</a></li> <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li> <li class="defaultLink"><a href="demos.html">Demos</a></li> <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </div> </div> </div> <div class="wrap"> <div class="toolbar"> <div class="breadcrumb toolblock"> <ul> <li class="first"><a href="index.html">Home</a></li> <!-- Breadcrumbs go here --> <li><a href="modules.html">Modules</a></li> <li>Qt3SupportLight</li> <li>Q3Semaphore</li> </ul> </div> <div class="toolbuttons toolblock"> <ul> <li id="smallA" class="t_button">A</li> <li id="medA" class="t_button active">A</li> <li id="bigA" class="t_button">A</li> <li id="print" class="t_button"><a href="javascript:this.print();"> <span>Print</span></a></li> </ul> </div> </div> <div class="content mainContent"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#public-functions">Public Functions</a></li> <li class="level1"><a href="#details">Detailed Description</a></li> </ul> </div> <h1 class="title">Q3Semaphore Class Reference</h1> <!-- $$$Q3Semaphore-brief --> <p>The Q3Semaphore class provides a robust integer semaphore. <a href="#details">More...</a></p> <!-- @@@Q3Semaphore --> <pre class="cpp"> <span class="preprocessor">#include <Q3Semaphore></span></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 <a href="porting4.html">Porting to Qt 4</a> for more information.</p> <p><b>Note:</b> All functions in this class are <a href="threads-reentrancy.html#thread-safe">thread-safe</a>.</p> <ul> <li><a href="q3semaphore-members.html">List of all members, including inherited members</a></li> </ul> <a name="public-functions"></a> <h2>Public Functions</h2> <table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="q3semaphore.html#Q3Semaphore">Q3Semaphore</a></b> ( int <i>maxcount</i> )</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="q3semaphore.html#dtor.Q3Semaphore">~Q3Semaphore</a></b> ()</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="q3semaphore.html#available">available</a></b> () const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="q3semaphore.html#total">total</a></b> () const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="q3semaphore.html#tryAccess">tryAccess</a></b> ( int <i>n</i> )</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="q3semaphore.html#operator-2b-2b">operator++</a></b> ( int )</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="q3semaphore.html#operator-2b-eq">operator+=</a></b> ( int <i>n</i> )</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="q3semaphore.html#operator--">operator--</a></b> ( int )</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="q3semaphore.html#operator--eq">operator-=</a></b> ( int <i>n</i> )</td></tr> </table> <a name="details"></a> <!-- $$$Q3Semaphore-description --> <div class="descr"> <h2>Detailed Description</h2> <p>The Q3Semaphore class provides a robust integer semaphore.</p> <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> <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> <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> <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> </div> <!-- @@@Q3Semaphore --> <div class="func"> <h2>Member Function Documentation</h2> <!-- $$$Q3Semaphore[overload1]$$$Q3Semaphoreint --> <h3 class="fn"><a name="Q3Semaphore"></a>Q3Semaphore::<span class="name">Q3Semaphore</span> ( <span class="type">int</span> <i>maxcount</i> )</h3> <p>Creates a new semaphore. The semaphore can be concurrently accessed at most <i>maxcount</i> times.</p> <!-- @@@Q3Semaphore --> <!-- $$$~Q3Semaphore[overload1]$$$~Q3Semaphore --> <h3 class="fn"><a name="dtor.Q3Semaphore"></a>Q3Semaphore::<span class="name">~Q3Semaphore</span> ()<tt> [virtual]</tt></h3> <p>Destroys the semaphore.</p> <p><b>Warning:</b> If you destroy a semaphore that has accesses in use the resultant behavior is undefined.</p> <!-- @@@~Q3Semaphore --> <!-- $$$available[overload1]$$$available --> <h3 class="fn"><a name="available"></a><span class="type">int</span> Q3Semaphore::<span class="name">available</span> () const</h3> <p>Returns the number of accesses currently available to the semaphore.</p> <!-- @@@available --> <!-- $$$total[overload1]$$$total --> <h3 class="fn"><a name="total"></a><span class="type">int</span> Q3Semaphore::<span class="name">total</span> () const</h3> <p>Returns the total number of accesses to the semaphore.</p> <!-- @@@total --> <!-- $$$tryAccess[overload1]$$$tryAccessint --> <h3 class="fn"><a name="tryAccess"></a><span class="type">bool</span> Q3Semaphore::<span class="name">tryAccess</span> ( <span class="type">int</span> <i>n</i> )</h3> <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> <!-- @@@tryAccess --> <!-- $$$operator++[overload1]$$$operator++int --> <h3 class="fn"><a name="operator-2b-2b"></a><span class="type">int</span> Q3Semaphore::<span class="name">operator++</span> ( <span class="type">int</span> )</h3> <p>Postfix ++ operator.</p> <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> <!-- @@@operator++ --> <!-- $$$operator+=[overload1]$$$operator+=int --> <h3 class="fn"><a name="operator-2b-eq"></a><span class="type">int</span> Q3Semaphore::<span class="name">operator+=</span> ( <span class="type">int</span> <i>n</i> )</h3> <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> <!-- @@@operator+= --> <!-- $$$operator--[overload1]$$$operator--int --> <h3 class="fn"><a name="operator--"></a><span class="type">int</span> Q3Semaphore::<span class="name">operator--</span> ( <span class="type">int</span> )</h3> <p>Postfix -- operator.</p> <p>Release access of the semaphore. This wakes all threads waiting for access to the semaphore.</p> <!-- @@@operator-- --> <!-- $$$operator-=[overload1]$$$operator-=int --> <h3 class="fn"><a name="operator--eq"></a><span class="type">int</span> Q3Semaphore::<span class="name">operator-=</span> ( <span class="type">int</span> <i>n</i> )</h3> <p>Release <i>n</i> accesses to the semaphore.</p> <!-- @@@operator-= --> </div> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2013 Digia Plc and/or its subsidiaries. Documentation contributions included herein are the copyrights of their respective owners.</p> <br /> <p> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.</p> <p> Documentation sources may be obtained from <a href="http://www.qt-project.org"> www.qt-project.org</a>.</p> <br /> <p> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. <a title="Privacy Policy" href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p> </div> <script src="scripts/functions.js" type="text/javascript"></script> </body> </html>