<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>ost::Semaphore class Reference</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body bgcolor="#ffffff"> <!-- Generated by Doxygen 1.2.10 --> <center> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="classes.html">Alphabetical List</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="namespacemembers.html">Namespace Members</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> </center> <hr><h1>ost::Semaphore Class Reference</h1>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 class="el" href="classost_1_1_semaphore.html">Semaphore</a> counter for thread synchronization. <a href="#_details">More...</a> <p> <code>#include <<a class="el" href="thread_8h-source.html">thread.h</a>></code> <p> <a href="classost_1_1_semaphore-members.html">List of all members.</a><table border=0 cellpadding=0 cellspacing=0> <tr><td colspan=2><br><h2>Public Methods</h2></td></tr> <tr><td nowrap align=right valign=top> </td><td valign=bottom><a class="el" href="classost_1_1_semaphore.html#a0">Semaphore</a> (size_t resource=0)</td></tr> <tr><td> </td><td><font size=-1><em>The initial value of the semaphore can be specified.</em> <a href="#a0">More...</a><em></em></font><br><br></td></tr> <tr><td nowrap align=right valign=top>virtual </td><td valign=bottom><a class="el" href="classost_1_1_semaphore.html#a1">~Semaphore</a> ()</td></tr> <tr><td> </td><td><font size=-1><em>Destroying a semaphore also removes any system resources associated with it.</em> <a href="#a1">More...</a><em></em></font><br><br></td></tr> <tr><td nowrap align=right valign=top>void </td><td valign=bottom><a class="el" href="classost_1_1_semaphore.html#a2">Wait</a> (void)</td></tr> <tr><td> </td><td><font size=-1><em>Wait is used to keep a thread held until the semaphore counter is greater than 0.</em> <a href="#a2">More...</a><em></em></font><br><br></td></tr> <tr><td nowrap align=right valign=top>bool </td><td valign=bottom><a class="el" href="classost_1_1_semaphore.html#a3">TryWait</a> (void)</td></tr> <tr><td> </td><td><font size=-1><em>TryWait is a non-blocking variant of Wait.</em> <a href="#a3">More...</a><em></em></font><br><br></td></tr> <tr><td nowrap align=right valign=top>void </td><td valign=bottom><a class="el" href="classost_1_1_semaphore.html#a4">Post</a> (void)</td></tr> <tr><td> </td><td><font size=-1><em>Posting to a semaphore increments its current value and releases the first thread waiting for the semaphore if it is currently at 0.</em> <a href="#a4">More...</a><em></em></font><br><br></td></tr> <tr><td nowrap align=right valign=top>int </td><td valign=bottom><a class="el" href="classost_1_1_semaphore.html#a5">getValue</a> (void)</td></tr> <tr><td> </td><td><font size=-1><em>Get the current value of a semaphore.</em> <a href="#a5">More...</a><em></em></font><br><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. <a class="el" href="classost_1_1_semaphore.html">Semaphore</a> 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><dd> David Sugar <<a href="mailto:dyfet@ostel.com">dyfet@ostel.com</a>> </dl> <p> <hr><h2>Constructor & Destructor Documentation</h2> <a name="a0" doxytag="ost::Semaphore::Semaphore"></a><p> <table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr> <td class="md"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> ost::Semaphore::Semaphore </td> <td class="md">( </td> <td class="md">size_t</td> <td class="mdname1"> <em>resource</em> = 0 </td> <td class="md">) </td> <td class="md"></td> </tr> </table> </td> </tr> </table> <table cellspacing=5 cellpadding=0 border=0> <tr> <td> </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.<dl compact><dt><b> Parameters: </b><dd> <table border=0 cellspacing=2 cellpadding=0> <tr><td valign=top><em>resource </em> </td><td> specify initial resource count or 0 default. </td></tr> </table> </dl> </td> </tr> </table> <a name="a1" doxytag="ost::Semaphore::~Semaphore"></a><p> <table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr> <td class="md"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> virtual ost::Semaphore::~Semaphore </td> <td class="md">( </td> <td class="mdname1"> </td> <td class="md">) </td> <td class="md"><code> [virtual]</code></td> </tr> </table> </td> </tr> </table> <table cellspacing=5 cellpadding=0 border=0> <tr> <td> </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 name="a4" doxytag="ost::Semaphore::Post"></a><p> <table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr> <td class="md"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> void ost::Semaphore::Post </td> <td class="md">( </td> <td class="md">void</td> <td class="mdname1"> </td> <td class="md">) </td> <td class="md"></td> </tr> </table> </td> </tr> </table> <table cellspacing=5 cellpadding=0 border=0> <tr> <td> </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><dd> <a class="el" href="classost_1_1_semaphore.html#a2">Wait</a> </dl> </td> </tr> </table> <a name="a3" doxytag="ost::Semaphore::TryWait"></a><p> <table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr> <td class="md"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> bool ost::Semaphore::TryWait </td> <td class="md">( </td> <td class="md">void</td> <td class="mdname1"> </td> <td class="md">) </td> <td class="md"></td> </tr> </table> </td> </tr> </table> <table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td> <p> TryWait is a non-blocking variant of Wait. <p> If the semaphore counter is greater than 0, then the thread is accepted and the semaphore counter is decreased. If the semaphore counter is 0 TryWait returns immediately with false. <p> <dl compact><dt><b> Returns: </b><dd> true if thread is accepted otherwise false</dl><dl compact><dt><b> See also: </b><dd> <a class="el" href="classost_1_1_semaphore.html#a2">Wait</a> , <a class="el" href="classost_1_1_semaphore.html#a4">Post</a> </dl> </td> </tr> </table> <a name="a2" doxytag="ost::Semaphore::Wait"></a><p> <table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr> <td class="md"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> void ost::Semaphore::Wait </td> <td class="md">( </td> <td class="md">void</td> <td class="mdname1"> </td> <td class="md">) </td> <td class="md"></td> </tr> </table> </td> </tr> </table> <table cellspacing=5 cellpadding=0 border=0> <tr> <td> </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> See also: </b><dd> <a class="el" href="classost_1_1_semaphore.html#a4">Post</a> </dl> </td> </tr> </table> <a name="a5" doxytag="ost::Semaphore::getValue"></a><p> <table width="100%" cellpadding="2" cellspacing="0" border="0"> <tr> <td class="md"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> int ost::Semaphore::getValue </td> <td class="md">( </td> <td class="md">void</td> <td class="mdname1"> </td> <td class="md">) </td> <td class="md"></td> </tr> </table> </td> </tr> </table> <table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td> <p> Get the current value of a semaphore. <p> <dl compact><dt><b> Returns: </b><dd> current value. </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><address><small>Generated at Fri Jan 4 18:51:50 2002 for CommonC++ by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.gif" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.10 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>, © 1997-2001</small></address> </body> </html>