Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 5448

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- qwaitcondition.qdoc -->
<head>
  <title>Qt 4.6: QWaitCondition Class Reference</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<a name="//apple_ref/cpp/cl//QWaitCondition"></a>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">QWaitCondition Class Reference<br /><span class="small-subtitle">[<a href="qtcore.html">QtCore</a> module]</span>
</h1>
<p>The QWaitCondition class provides a condition variable for synchronizing threads. <a href="#details">More...</a></p>
<pre> #include &lt;QWaitCondition&gt;</pre><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="qwaitcondition-members.html">List of all members, including inherited members</a></li>
</ul>
<hr />
<a name="public-functions"></a>
<h2>Public Functions</h2>
<table class="alignedsummary" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td class="memItemLeft" align="right" valign="top"></td><td class="memItemRight" valign="bottom"><b><a href="qwaitcondition.html#QWaitCondition">QWaitCondition</a></b> ()</td></tr>
<tr><td class="memItemLeft" align="right" valign="top"></td><td class="memItemRight" valign="bottom"><b><a href="qwaitcondition.html#dtor.QWaitCondition">~QWaitCondition</a></b> ()</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><b><a href="qwaitcondition.html#wait">wait</a></b> ( QMutex * <i>mutex</i>, unsigned long <i>time</i> = ULONG_MAX )</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><b><a href="qwaitcondition.html#wait-2">wait</a></b> ( QReadWriteLock * <i>readWriteLock</i>, unsigned long <i>time</i> = ULONG_MAX )</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><b><a href="qwaitcondition.html#wakeAll">wakeAll</a></b> ()</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><b><a href="qwaitcondition.html#wakeOne">wakeOne</a></b> ()</td></tr>
</table>
<a name="details"></a>
<hr />
<h2>Detailed Description</h2>
<p>The QWaitCondition class provides a condition variable for synchronizing threads.</p>
<p>QWaitCondition allows a thread to tell other threads that some sort of condition has been met. One or many threads can block waiting for a QWaitCondition to set a condition with <a href="qwaitcondition.html#wakeOne">wakeOne</a>() or <a href="qwaitcondition.html#wakeAll">wakeAll</a>(). Use <a href="qwaitcondition.html#wakeOne">wakeOne</a>() to wake one randomly selected condition or <a href="qwaitcondition.html#wakeAll">wakeAll</a>() to wake them all.</p>
<p>For example, let's suppose that we have three tasks that should be performed whenever the user presses a key. Each task could be split into a thread, each of which would have a <a href="qthread.html#run">run()</a> body like this:</p>
<pre> forever {
     mutex.lock();
     keyPressed.wait(&amp;mutex);
     do_something();
     mutex.unlock();
 }</pre>
<p>Here, the <tt>keyPressed</tt> variable is a global variable of type QWaitCondition.</p>
<p>A fourth thread would read key presses and wake the other three threads up every time it receives one, like this:</p>
<pre> forever {
     getchar();
     keyPressed.wakeAll();
 }</pre>
<p>The order in which the three threads are woken up is undefined. Also, if some of the threads are still in <tt>do_something()</tt> when the key is pressed, they won't be woken up (since they're not waiting on the condition variable) and so the task will not be performed for that key press. This issue can be solved using a counter and a <a href="qmutex.html">QMutex</a> to guard it. For example, here's the new code for the worker threads:</p>
<pre> forever {
     mutex.lock();
     keyPressed.wait(&amp;mutex);
     ++count;
     mutex.unlock();

     do_something();

     mutex.lock();
     --count;
     mutex.unlock();
 }</pre>
<p>Here's the code for the fourth thread:</p>
<pre> forever {
     getchar();

     mutex.lock();
     <span class="comment">// Sleep until there are no busy worker threads</span>
     while (count &gt; 0) {
         mutex.unlock();
         sleep(1);
         mutex.lock();
     }
     keyPressed.wakeAll();
     mutex.unlock();
 }</pre>
<p>The mutex is necessary because the results of two threads attempting to change the value of the same variable simultaneously are unpredictable.</p>
<p>Wait conditions are a powerful thread synchronization primitive. The <a href="threads-waitconditions.html">Wait Conditions</a> example shows how to use QWaitCondition as an alternative to <a href="qsemaphore.html">QSemaphore</a> for controlling access to a circular buffer shared by a producer thread and a consumer thread.</p>
<p>See also <a href="qmutex.html">QMutex</a>, <a href="qsemaphore.html">QSemaphore</a>, <a href="qthread.html">QThread</a>, and <a href="threads-waitconditions.html">Wait Conditions Example</a>.</p>
<hr />
<h2>Member Function Documentation</h2>
<a name="//apple_ref/cpp/instm/QWaitCondition/QWaitCondition"></a>
<h3 class="fn"><a name="QWaitCondition"></a>QWaitCondition::QWaitCondition ()</h3>
<p>Constructs a new wait condition object.</p>
<a name="//apple_ref/cpp/instm/QWaitCondition/~QWaitCondition"></a>
<h3 class="fn"><a name="dtor.QWaitCondition"></a>QWaitCondition::~QWaitCondition ()</h3>
<p>Destroys the wait condition object.</p>
<a name="//apple_ref/cpp/instm/QWaitCondition/wait"></a>
<h3 class="fn"><a name="wait"></a>bool QWaitCondition::wait ( <a href="qmutex.html">QMutex</a> * <i>mutex</i>, unsigned long <i>time</i> = ULONG_MAX )</h3>
<p>Releases the locked <i>mutex</i> and waits on the wait condition. The <i>mutex</i> must be initially locked by the calling thread. If <i>mutex</i> is not in a locked state, this function returns immediately. If <i>mutex</i> is a recursive mutex, this function returns immediately. The <i>mutex</i> will be unlocked, and the calling thread will block until either of these conditions is met:</p>
<ul>
<li>Another thread signals it using <a href="qwaitcondition.html#wakeOne">wakeOne</a>() or <a href="qwaitcondition.html#wakeAll">wakeAll</a>(). This function will return true in this case.</li>
<li><i>time</i> milliseconds has elapsed. If <i>time</i> is <tt>ULONG_MAX</tt> (the default), then the wait will never timeout (the event must be signalled). This function will return false if the wait timed out.</li>
</ul>
<p>The mutex will be returned to the same locked state. This function is provided to allow the atomic transition from the locked state to the wait state.</p>
<p>See also <a href="qwaitcondition.html#wakeOne">wakeOne</a>() and <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>
<h3 class="fn"><a name="wait-2"></a>bool QWaitCondition::wait ( <a href="qreadwritelock.html">QReadWriteLock</a> * <i>readWriteLock</i>, unsigned long <i>time</i> = ULONG_MAX )</h3>
<p>Releases the locked <i>readWriteLock</i> and waits on the wait condition. The <i>readWriteLock</i> must be initially locked by the calling thread. If <i>readWriteLock</i> is not in a locked state, this function returns immediately. The <i>readWriteLock</i> must not be locked recursively, otherwise this function will not release the lock properly. The <i>readWriteLock</i> will be unlocked, and the calling thread will block until either of these conditions is met:</p>
<ul>
<li>Another thread signals it using <a href="qwaitcondition.html#wakeOne">wakeOne</a>() or <a href="qwaitcondition.html#wakeAll">wakeAll</a>(). This function will return true in this case.</li>
<li><i>time</i> milliseconds has elapsed. If <i>time</i> is <tt>ULONG_MAX</tt> (the default), then the wait will never timeout (the event must be signalled). This function will return false if the wait timed out.</li>
</ul>
<p>The <i>readWriteLock</i> will be returned to the same locked state. This function is provided to allow the atomic transition from the locked state to the wait state.</p>
<p>This function was introduced in Qt 4.4.</p>
<p>See also <a href="qwaitcondition.html#wakeOne">wakeOne</a>() and <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>
<a name="//apple_ref/cpp/instm/QWaitCondition/wakeAll"></a>
<h3 class="fn"><a name="wakeAll"></a>void QWaitCondition::wakeAll ()</h3>
<p>Wakes all threads waiting on the wait condition. The order in which the threads are woken up depends on the operating system's scheduling policies and cannot be controlled or predicted.</p>
<p>See also <a href="qwaitcondition.html#wakeOne">wakeOne</a>().</p>
<a name="//apple_ref/cpp/instm/QWaitCondition/wakeOne"></a>
<h3 class="fn"><a name="wakeOne"></a>void QWaitCondition::wakeOne ()</h3>
<p>Wakes one thread waiting on the wait condition. The thread that is woken up depends on the operating system's scheduling policies, and cannot be controlled or predicted.</p>
<p>If you want to wake up a specific thread, the solution is typically to use different wait conditions and have different threads wait on different conditions.</p>
<p>See also <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>