⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cond.html

📁 多线程库
💻 HTML
字号:
<HTML><HEAD><TITLE>cond Class</TITLE></HEAD><BODY bgcolor="#ffffff"><H1>cond Class Reference</H1><p>[<A HREF="index.html">threads Index</A>] [<A HREF="heir.html">threads Hierarchy</A>]</p><HR><P>Class for signalling conditions between processes.   <a href="#short">More...</a></P><P><code>	#include &lt;<a href="-usr-local-include-thread_cond-h.html">/usr/local/include/thread_cond.h</a>&gt;</code></P><H2>Public Members</H2><UL><LI><b><a name="ref0">cond</a></b> (attributes::scope) </LI><LI><b><a name="ref1">cond</a></b> () </LI><LI><b><a name="ref2">~cond</a></b> () </LI><LI>attributes::scope <b><a href="#ref3">scope</a></b> () </LI><LI>int <b><a href="#ref4">wait</a></b> (mutex&amp;) </LI><LI>int <b><a href="#ref5">timedwait</a></b> (mutex&amp;, const struct timespec *) </LI><LI>int <b><a href="#ref6">timedwait_rel</a></b> (mutex&amp;, const struct timespec *) </LI><LI>int <b><a href="#ref7">signal</a></b> () </LI><LI>int <b><a href="#ref8">broadcast</a></b> () </LI><LI>static void <b><a href="#ref9">project_part</a></b> (const char *) </LI></UL><HR><H2><a name="short">Detailed Description</a></H2><P> Cond, is a conditional semaphore.  A process can wait on a specific condition... and another, can trigger that condition through this class.</p><p> One typical example of use of this kind of class, is the producer consumer example:</p><p> <pre></p><p> #include <thread.h> #include <string></p><p> string produced; mutex lock; cond signal;</p><p> class producer : public pthread {  public:    producer() { };    ~producer() { };    int thread(void *) {       char buf[80];</p><p>       lock.lock();       cin.geline(buf,80);       produced = buf;       signal.signal();       lock.unlock();    }; };</p><p> class consumer : public pthread {  public:    consumer() { };    ~consumer() { };</p><p>    int thread(void *) {       lock.lock();       signal.wait();       cout << "Production was '" << s.c_str() << "'\n";       lock.unlock();    } }</p><p> main() {   pthread = p;    p new consumer;   (void)new producer;   p.join(); }</p><p> </pre></p><p> Notice the order, in which the two threads are created.  The consumer before the producer.  This is because both of these threads start by locking the mutex lock, and the consumer must be the first one to actually acquire it. </P><HR><H2>attributes::scope <a name="ref3"></a><a name="scope">scope</a>()  </H2><p> Each condition variable, can have one of the following scopes.</p><p> <pre> process_shared     - interprocess process_private    - only within this process and its threads. </pre></p><p></p><dl><dt><b>Returns</b>:<dd>The scope of this condition variable.</dl><H2>int <a name="ref4"></a><a name="wait">wait</a>(<a href="mutex.html">mutex</a>&amp;)  </H2><p> This method, waits until a signal has been received and suspends the calling process during that period.  The thread can be cancelled, while waiting for the signal.</p><p></p><dl><dt><b>Parameters</b>:<dd><table width="100%" border="0"><tr><td align="left" valign="top">mutex</td><td align="left" valign="top">A mutex variable, which is unlocked during the period and locked immediately after.</td></tr></table></dl><dl><dt><b>Returns</b>:<dd>Always returns 0, signifying success.</dl><H2>int <a name="ref5"></a><a name="timedwait">timedwait</a>(<a href="mutex.html">mutex</a>&amp;, const struct timespec *)  </H2><p> This method, acts in a similar manner to the one above, but it will timeout on the wait when a specific time tick has been reached. Take a look at <a href="cond.html#timedwait_rel">timedwait_rel</a>  for a discussion on possible return values. of return values that are available.</p><p></p><dl><dt><b>Parameters</b>:<dd><table width="100%" border="0"><tr><td align="left" valign="top">timespec</td><td align="left" valign="top">The time tick, when a timeout should occur.</td></tr><tr><td align="left" valign="top">mutex</td><td align="left" valign="top">Mutex variable to lock on signal.</td></tr></table></dl><dl><dt><b>See Also</b>:<dd><a href="cond.html#timedwait_rel">timedwait_rel</a></dl><H2>int <a name="ref6"></a><a name="timedwait_rel">timedwait_rel</a>(<a href="mutex.html">mutex</a>&amp;, const struct timespec *)  </H2><p> This method is equivalent to the above, except that the time specification it accepts is a relative time.  Instead of specifying the absolute time tick to timeout, it specifies the amount of time to wait, until a timeout should occurr. This method will return a value, specifying the cause of return from it.  It can be one of: <pre>   ESRCH          - If the calling thread is not known to the system.   EINTR          - If an interrupt signal was received and terminated the wait.   ETIMEOUT       - If a timeout occurred, prior to receiving a signal.   0              - On success. </pre></p><p></p><dl><dt><b>Parameters</b>:<dd><table width="100%" border="0"><tr><td align="left" valign="top">timespec</td><td align="left" valign="top">A structure with the amount of time to wait for a signal, before timing out.</td></tr><tr><td align="left" valign="top">mutex</td><td align="left" valign="top">The mutex to lock, when signal has been received.</td></tr></table></dl><H2>int <a name="ref7"></a><a name="signal">signal</a>()  </H2><p> This method, will send a signal to the next process that is registered in the waiting processes list.  That process will be revived.</p><p></p><dl><dt><b>Returns</b>:<dd>Always return 0, signifying success.</dl><H2>int <a name="ref8"></a><a name="broadcast">broadcast</a>()  </H2><p> This method, will send a signal to every process on the waiting processes list, reviving them all.  All processes will be removed from the waiting queue.</p><p></p><dl><dt><b>Returns</b>:<dd>Always return 0, signifying success.</dl><H2>static void <a name="ref9"></a><a name="project_part">project_part</a>(const char *)  </H2><p> The shared memory scheme, wants a single name to identify the overall program.  It is possible, and perhaps desired that part cond/mutex/semaphore have their own name identification tree.  This is possible, by stating that it should be branch of the main file name, with a give extension.</p><p> <pre> main() {   cond *cv;</p><p>   pthread::set_project( "my_project" );   cond::project_par( "cond" );   cv = new cond(attributes::process_shared);   ... } </pre></p><p> This will set the project to my_project, and all condition variables will be derived from my_project_cond.</p><p></p><dl><dt><b>Parameters</b>:<dd><table width="100%" border="0"><tr><td align="left" valign="top">part</td><td align="left" valign="top">A C string containing the cond part name.</td></tr></table></dl><HR><TABLE WIDTH="100%"><TR><TD ALIGN="left" VALIGN="top"><UL><LI><I>Author</I>: Orn Hansen &lt;oe.hansen@gamma.telenordia.se&gt; </LI><LI>Documentation generated by oehansen@citadel on Mi

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -