<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <meta name="generator" content="Doxygen 1.8.5"/> <title>GNU CommonC++: ost::Semaphore Class Reference</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="dynsections.js"></script> <link href="doxygen.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td style="padding-left: 0.5em;"> <div id="projectname">GNU CommonC++ </div> </td> </tr> </tbody> </table> </div> <!-- end header part --> <!-- Generated by Doxygen 1.8.5 --> <div id="navrow1" class="tabs"> <ul class="tablist"> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li class="current"><a href="annotated.html"><span>Classes</span></a></li> <li><a href="files.html"><span>Files</span></a></li> <li><a href="examples.html"><span>Examples</span></a></li> </ul> </div> <div id="navrow2" class="tabs2"> <ul class="tablist"> <li><a href="annotated.html"><span>Class List</span></a></li> <li><a href="classes.html"><span>Class Index</span></a></li> <li><a href="hierarchy.html"><span>Class Hierarchy</span></a></li> <li><a href="functions.html"><span>Class Members</span></a></li> </ul> </div> <div id="nav-path" class="navpath"> <ul> <li class="navelem"><a class="el" href="namespaceost.html">ost</a></li><li class="navelem"><a class="el" href="classost_1_1_semaphore.html">Semaphore</a></li> </ul> </div> </div><!-- top --> <div class="header"> <div class="summary"> <a href="#pub-methods">Public Member Functions</a> | <a href="classost_1_1_semaphore-members.html">List of all members</a> </div> <div class="headertitle"> <div class="title">ost::Semaphore Class Reference</div> </div> </div><!--header--> <div class="contents"> <p>A semaphore is generally used as a synchronization object between multiple threads or to protect a limited and finite resource such as a memory or thread pool. <a href="classost_1_1_semaphore.html#details">More...</a></p> <p><code>#include <<a class="el" href="thread_8h_source.html">thread.h</a>></code></p> <div class="dynheader"> Inheritance diagram for ost::Semaphore:</div> <div class="dyncontent"> <div class="center"> <img src="classost_1_1_semaphore.png" usemap="#ost::Semaphore_map" alt=""/> <map id="ost::Semaphore_map" name="ost::Semaphore_map"> <area href="classost_1_1_thread_queue.html" title="Somewhat generic queue processing class to establish a producer consumer queue. " alt="ost::ThreadQueue" shape="rect" coords="0,56,112,80"/> </map> </div></div> <table class="memberdecls"> <tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="pub-methods"></a> Public Member Functions</h2></td></tr> <tr class="memitem:a0a6f933da386bbe01b2b0b6b266bcc85"><td class="memItemLeft" align="right" valign="top"> </td><td class="memItemRight" valign="bottom"><a class="el" href="classost_1_1_semaphore.html#a0a6f933da386bbe01b2b0b6b266bcc85">Semaphore</a> (unsigned resource=0)</td></tr> <tr class="memdesc:a0a6f933da386bbe01b2b0b6b266bcc85"><td class="mdescLeft"> </td><td class="mdescRight">The initial value of the semaphore can be specified. <a href="#a0a6f933da386bbe01b2b0b6b266bcc85">More...</a><br/></td></tr> <tr class="separator:a0a6f933da386bbe01b2b0b6b266bcc85"><td class="memSeparator" colspan="2"> </td></tr> <tr class="memitem:a138de594478446aa07f3bad15dce3846"><td class="memItemLeft" align="right" valign="top">virtual </td><td class="memItemRight" valign="bottom"><a class="el" href="classost_1_1_semaphore.html#a138de594478446aa07f3bad15dce3846">~Semaphore</a> ()</td></tr> <tr class="memdesc:a138de594478446aa07f3bad15dce3846"><td class="mdescLeft"> </td><td class="mdescRight">Destroying a semaphore also removes any system resources associated with it. <a href="#a138de594478446aa07f3bad15dce3846">More...</a><br/></td></tr> <tr class="separator:a138de594478446aa07f3bad15dce3846"><td class="memSeparator" colspan="2"> </td></tr> <tr class="memitem:a1f2526be16a463d2392a74f668a91d80"><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><a class="el" href="classost_1_1_semaphore.html#a1f2526be16a463d2392a74f668a91d80">wait</a> (<a class="el" href="thread_8h.html#af412159e5cef839836a5e7b19ee75d1c">timeout_t</a> timeout=0)</td></tr> <tr class="memdesc:a1f2526be16a463d2392a74f668a91d80"><td class="mdescLeft"> </td><td class="mdescRight">Wait is used to keep a thread held until the semaphore counter is greater than 0. <a href="#a1f2526be16a463d2392a74f668a91d80">More...</a><br/></td></tr> <tr class="separator:a1f2526be16a463d2392a74f668a91d80"><td class="memSeparator" colspan="2"> </td></tr> <tr class="memitem:a7535c9c3f3c60596b21b5275342cbe1e"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="classost_1_1_semaphore.html#a7535c9c3f3c60596b21b5275342cbe1e">post</a> (void)</td></tr> <tr class="memdesc:a7535c9c3f3c60596b21b5275342cbe1e"><td class="mdescLeft"> </td><td class="mdescRight">Posting to a semaphore increments its current value and releases the first thread waiting for the semaphore if it is currently at 0. <a href="#a7535c9c3f3c60596b21b5275342cbe1e">More...</a><br/></td></tr> <tr class="separator:a7535c9c3f3c60596b21b5275342cbe1e"><td class="memSeparator" colspan="2"> </td></tr> <tr class="memitem:a23c458d249a3f68b83deaa5c7986dfca"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="classost_1_1_semaphore.html#a23c458d249a3f68b83deaa5c7986dfca">force_unlock_after_cancellation</a> ()</td></tr> <tr class="memdesc:a23c458d249a3f68b83deaa5c7986dfca"><td class="mdescLeft"> </td><td class="mdescRight">Call it after a deferred cancellation to avoid deadlocks. <a href="#a23c458d249a3f68b83deaa5c7986dfca">More...</a><br/></td></tr> <tr class="separator:a23c458d249a3f68b83deaa5c7986dfca"><td class="memSeparator" colspan="2"> </td></tr> </table> <a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2> <div class="textblock"><p>A semaphore is generally used as a synchronization object between multiple threads or to protect a limited and finite resource such as a memory or thread pool. </p> <p>The semaphore has a counter which only permits access by one or more threads when the value of the semaphore is non-zero. Each access reduces the current value of the semaphore by 1. One or more threads can wait on a semaphore until it is no longer 0, and hence the semaphore can be used as a simple thread synchronization object to enable one thread to pause others until the thread is ready or has provided data for them. Semaphores are typically used as a counter for protecting or limiting concurrent access to a given resource, such as to permitting at most "x" number of threads to use resource "y", for example.</p> <dl class="section author"><dt>Author</dt><dd>David Sugar <a href="#" onclick="location.href='mai'+'lto:'+'dyf'+'et'+'@os'+'te'+'l.c'+'om'; return false;">dyfet<span style="display: none;">.nosp@m.</span>@ost<span style="display: none;">.nosp@m.</span>el.co<span style="display: none;">.nosp@m.</span>m</a> <a class="el" href="classost_1_1_semaphore.html" title="A semaphore is generally used as a synchronization object between multiple threads or to protect a li...">Semaphore</a> counter for thread synchronization. </dd></dl> <dl><dt><b>Examples: </b></dt><dd><a class="el" href="tcpservice_8cpp-example.html#_a5">tcpservice.cpp</a>.</dd> </dl></div><h2 class="groupheader">Constructor & Destructor Documentation</h2> <a class="anchor" id="a0a6f933da386bbe01b2b0b6b266bcc85"></a> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">ost::Semaphore::Semaphore </td> <td>(</td> <td class="paramtype">unsigned </td> <td class="paramname"><em>resource</em> = <code>0</code></td><td>)</td> <td></td> </tr> </table> </div><div class="memdoc"> <p>The initial value of the semaphore can be specified. </p> <p>An initial value is often used When used to lock a finite resource or to specify the maximum number of thread instances that can access a specified resource.</p> <dl class="params"><dt>Parameters</dt><dd> <table class="params"> <tr><td class="paramname">resource</td><td>specify initial resource count or 0 default. </td></tr> </table> </dd> </dl> </div> </div> <a class="anchor" id="a138de594478446aa07f3bad15dce3846"></a> <div class="memitem"> <div class="memproto"> <table class="mlabels"> <tr> <td class="mlabels-left"> <table class="memname"> <tr> <td class="memname">virtual ost::Semaphore::~Semaphore </td> <td>(</td> <td class="paramname"></td><td>)</td> <td></td> </tr> </table> </td> <td class="mlabels-right"> <span class="mlabels"><span class="mlabel">virtual</span></span> </td> </tr> </table> </div><div class="memdoc"> <p>Destroying a semaphore also removes any system resources associated with it. </p> <p>If a semaphore has threads currently waiting on it, those threads will all continue when a semaphore is destroyed. </p> </div> </div> <h2 class="groupheader">Member Function Documentation</h2> <a class="anchor" id="a23c458d249a3f68b83deaa5c7986dfca"></a> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">void ost::Semaphore::force_unlock_after_cancellation </td> <td>(</td> <td class="paramname"></td><td>)</td> <td></td> </tr> </table> </div><div class="memdoc"> <p>Call it after a deferred cancellation to avoid deadlocks. </p> <p>From PTHREAD_COND_TIMEDWAIT(3P): A condition wait (whether timed or not) is a cancellation point. When the cancelability enable state of a thread is set to PTHREAD_CANCEL_DEFERRED, a side effect of acting upon a cancellation request while in a condition wait is that the mutex is (in effect) re-acquired before calling the first cancellation cleanup handler. </p> </div> </div> <a class="anchor" id="a7535c9c3f3c60596b21b5275342cbe1e"></a> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">void ost::Semaphore::post </td> <td>(</td> <td class="paramtype">void </td> <td class="paramname"></td><td>)</td> <td></td> </tr> </table> </div><div class="memdoc"> <p>Posting to a semaphore increments its current value and releases the first thread waiting for the semaphore if it is currently at 0. </p> <p>Interestingly, there is no support to increment a semaphore by any value greater than 1 to release multiple waiting threads in either pthread or the win32 API. Hence, if one wants to release a semaphore to enable multiple threads to execute, one must perform multiple post operations.</p> <dl class="section see"><dt>See Also</dt><dd><a class="el" href="classost_1_1_semaphore.html#a1f2526be16a463d2392a74f668a91d80" title="Wait is used to keep a thread held until the semaphore counter is greater than 0. ...">wait</a> </dd></dl> </div> </div> <a class="anchor" id="a1f2526be16a463d2392a74f668a91d80"></a> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">bool ost::Semaphore::wait </td> <td>(</td> <td class="paramtype"><a class="el" href="thread_8h.html#af412159e5cef839836a5e7b19ee75d1c">timeout_t</a> </td> <td class="paramname"><em>timeout</em> = <code>0</code></td><td>)</td> <td></td> </tr> </table> </div><div class="memdoc"> <p>Wait is used to keep a thread held until the semaphore counter is greater than 0. </p> <p>If the current thread is held, then another thread must increment the semaphore. Once the thread is accepted, the semaphore is automatically decremented, and the thread continues execution.</p> <p>The pthread semaphore object does not support a timed "wait", and hence to maintain consistancy, neither the posix nor win32 source trees support "timed" semaphore objects.</p> <dl class="section return"><dt>Returns</dt><dd>false if timed out </dd></dl> <dl class="params"><dt>Parameters</dt><dd> <table class="params"> <tr><td class="paramname">timeout</td><td>period in milliseconds to wait </td></tr> </table> </dd> </dl> <dl class="section see"><dt>See Also</dt><dd><a class="el" href="classost_1_1_semaphore.html#a7535c9c3f3c60596b21b5275342cbe1e" title="Posting to a semaphore increments its current value and releases the first thread waiting for the sem...">post</a> </dd></dl> </div> </div> <hr/>The documentation for this class was generated from the following file:<ul> <li><a class="el" href="thread_8h_source.html">thread.h</a></li> </ul> </div><!-- contents --> <!-- start footer part --> <hr class="footer"/><address class="footer"><small> Generated on Thu Oct 17 2013 23:24:58 for GNU CommonC++ by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/> </a> 1.8.5 </small></address> </body> </html>