Sophie

Sophie

distrib > Mandriva > 2007.0 > i586 > by-pkgid > ad1ba1135a9c9eeffc2e538163e00373 > files > 265

libCommonC++2_1.4-devel-1.4.1-1mdv2007.0.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>GNU CommonC++: ost::Semaphore Class Reference</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.6 -->
<div class="tabs">
  <ul>
    <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
    <li><a href="namespaces.html"><span>Namespaces</span></a></li>
    <li id="current"><a href="classes.html"><span>Classes</span></a></li>
    <li><a href="files.html"><span>Files</span></a></li>
    <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
    <li><a href="examples.html"><span>Examples</span></a></li>
  </ul></div>
<div class="tabs">
  <ul>
    <li><a href="classes.html"><span>Alphabetical&nbsp;List</span></a></li>
    <li><a href="annotated.html"><span>Class&nbsp;List</span></a></li>
    <li><a href="hierarchy.html"><span>Class&nbsp;Hierarchy</span></a></li>
    <li><a href="functions.html"><span>Class&nbsp;Members</span></a></li>
  </ul></div>
<div class="nav">
<a class="el" href="namespaceost.html">ost</a>::<a class="el" href="classost_1_1_semaphore.html">Semaphore</a></div>
<h1>ost::Semaphore Class Reference</h1><!-- doxytag: class="ost::Semaphore" -->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.Semaphore counter for thread synchronization.  
<a href="#_details">More...</a>
<p>
<code>#include &lt;<a class="el" href="thread_8h-source.html">thread.h</a>&gt;</code>
<p>
<p>Inheritance diagram for ost::Semaphore:
<p><center><img src="classost_1_1_semaphore.png" usemap="#ost::Semaphore_map" border="0" alt=""></center>
<map name="ost::Semaphore_map">
<area href="classost_1_1_thread_queue.html" alt="ost::ThreadQueue" shape="rect" coords="0,56,112,80">
</map>
<a href="classost_1_1_semaphore-members.html">List of all members.</a><table border="0" cellpadding="0" cellspacing="0">
<tr><td></td></tr>
<tr><td colspan="2"><br><h2>Public Member Functions</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classost_1_1_semaphore.html#0a6f933da386bbe01b2b0b6b266bcc85">Semaphore</a> (unsigned resource=0)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">The initial value of the semaphore can be specified.  <a href="#0a6f933da386bbe01b2b0b6b266bcc85"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classost_1_1_semaphore.html#138de594478446aa07f3bad15dce3846">~Semaphore</a> ()</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Destroying a semaphore also removes any system resources associated with it.  <a href="#138de594478446aa07f3bad15dce3846"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">bool&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classost_1_1_semaphore.html#1f2526be16a463d2392a74f668a91d80">wait</a> (<a class="el" href="thread_8h.html#f412159e5cef839836a5e7b19ee75d1c">timeout_t</a> timeout=0)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Wait is used to keep a thread held until the semaphore counter is greater than 0.  <a href="#1f2526be16a463d2392a74f668a91d80"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classost_1_1_semaphore.html#7535c9c3f3c60596b21b5275342cbe1e">post</a> (void)</td></tr>

<tr><td class="mdescLeft">&nbsp;</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="#7535c9c3f3c60596b21b5275342cbe1e"></a><br></td></tr>
</table>
<hr><a name="_details"></a><h2>Detailed Description</h2>
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.Semaphore counter for thread synchronization. 
<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 compact><dt><b>Author:</b></dt><dd>David Sugar &lt;<a href="mailto:dyfet@ostel.com">dyfet@ostel.com</a>&gt; </dd></dl>
<dl compact><dt><b>Examples: </b></dt><dd>

<p>
<a class="el" href="tcpservice_8cpp-example.html#_a59">tcpservice.cpp</a>.</dl>
<p>
<hr><h2>Constructor &amp; Destructor Documentation</h2>
<a class="anchor" name="0a6f933da386bbe01b2b0b6b266bcc85"></a><!-- doxytag: member="ost::Semaphore::Semaphore" ref="0a6f933da386bbe01b2b0b6b266bcc85" args="(unsigned resource=0)" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">ost::Semaphore::Semaphore           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top">unsigned&nbsp;</td>
          <td class="mdname1" valign="top" nowrap> <em>resource</em> = <code>0</code>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
The initial value of the semaphore can be specified. 
<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 compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>resource</em>&nbsp;</td><td>specify initial resource count or 0 default. </td></tr>
  </table>
</dl>
    </td>
  </tr>
</table>
<a class="anchor" name="138de594478446aa07f3bad15dce3846"></a><!-- doxytag: member="ost::Semaphore::~Semaphore" ref="138de594478446aa07f3bad15dce3846" args="()" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual ost::Semaphore::~Semaphore           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap><code> [virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Destroying a semaphore also removes any system resources associated with it. 
<p>
If a semaphore has threads currently waiting on it, those threads will all continue when a semaphore is destroyed.     </td>
  </tr>
</table>
<hr><h2>Member Function Documentation</h2>
<a class="anchor" name="7535c9c3f3c60596b21b5275342cbe1e"></a><!-- doxytag: member="ost::Semaphore::post" ref="7535c9c3f3c60596b21b5275342cbe1e" args="(void)" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">void ost::Semaphore::post           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top">void&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<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>
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 compact><dt><b>See also:</b></dt><dd><a class="el" href="classost_1_1_semaphore.html#1f2526be16a463d2392a74f668a91d80">wait</a> </dd></dl>
    </td>
  </tr>
</table>
<a class="anchor" name="1f2526be16a463d2392a74f668a91d80"></a><!-- doxytag: member="ost::Semaphore::wait" ref="1f2526be16a463d2392a74f668a91d80" args="(timeout_t timeout=0)" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">bool ost::Semaphore::wait           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top"><a class="el" href="thread_8h.html#f412159e5cef839836a5e7b19ee75d1c">timeout_t</a>&nbsp;</td>
          <td class="mdname1" valign="top" nowrap> <em>timeout</em> = <code>0</code>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Wait is used to keep a thread held until the semaphore counter is greater than 0. 
<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>
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 compact><dt><b>Returns:</b></dt><dd>false if timed out </dd></dl>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>timeout</em>&nbsp;</td><td>period in milliseconds to wait </td></tr>
  </table>
</dl>
<dl compact><dt><b>See also:</b></dt><dd><a class="el" href="classost_1_1_semaphore.html#7535c9c3f3c60596b21b5275342cbe1e">post</a> </dd></dl>
    </td>
  </tr>
</table>
<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></ul>
<hr size="1"><address style="align: right;"><small>Generated on Mon Jun 19 10:50:57 2006 for GNU CommonC++ by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address>
</body>
</html>