Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > a6711891ce757817bba854bf3f25205a > files > 2446

qtjambi-doc-4.3.3-3mdv2008.1.i586.rpm

<class name="QWaitCondition" doc="/**
&lt;p&gt;The &lt;a href=&quot;QWaitCondition.html#QWaitCondition()&quot;&gt;&lt;tt&gt;QWaitCondition&lt;/tt&gt;&lt;/a&gt; class provides a condition variable for synchronizing threads.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;QWaitCondition.html#QWaitCondition()&quot;&gt;&lt;tt&gt;QWaitCondition&lt;/tt&gt;&lt;/a&gt; allows a thread to tell other threads that some sort of condition has been met. One or many threads can block waiting for a &lt;a href=&quot;QWaitCondition.html#QWaitCondition()&quot;&gt;&lt;tt&gt;QWaitCondition&lt;/tt&gt;&lt;/a&gt; to set a condition with &lt;a href=&quot;QWaitCondition.html#wakeOne()&quot;&gt;&lt;tt&gt;wakeOne&lt;/tt&gt;&lt;/a&gt; or &lt;a href=&quot;QWaitCondition.html#wakeAll()&quot;&gt;&lt;tt&gt;wakeAll&lt;/tt&gt;&lt;/a&gt;. Use &lt;a href=&quot;QWaitCondition.html#wakeOne()&quot;&gt;&lt;tt&gt;wakeOne&lt;/tt&gt;&lt;/a&gt; to wake one randomly selected condition or &lt;a href=&quot;QWaitCondition.html#wakeAll()&quot;&gt;&lt;tt&gt;wakeAll&lt;/tt&gt;&lt;/a&gt; to wake them all.&lt;/p&gt;
&lt;p&gt;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 run() body like this:&lt;/p&gt;
&lt;pre&gt;    forever {
        mutex.lock();
        keyPressed.wait(&amp;amp;mutex);
        do_something();
        mutex.unlock();
    }&lt;/pre&gt;
&lt;p&gt;Here, the &lt;tt&gt;keyPressed&lt;/tt&gt; variable is a global variable of type &lt;a href=&quot;QWaitCondition.html#QWaitCondition()&quot;&gt;&lt;tt&gt;QWaitCondition&lt;/tt&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;A fourth thread would read key presses and wake the other three threads up every time it receives one, like this:&lt;/p&gt;
&lt;pre&gt;    forever {
        getchar();
        keyPressed.wakeAll();
    }&lt;/pre&gt;
&lt;p&gt;The order in which the three threads are woken up is undefined. Also, if some of the threads are still in &lt;tt&gt;do_something()&lt;/tt&gt; 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 &lt;a href=&quot;QMutex.html&quot;&gt;&lt;tt&gt;QMutex&lt;/tt&gt;&lt;/a&gt; to guard it. For example, here's the new code for the worker threads:&lt;/p&gt;
&lt;pre&gt;    forever {
        mutex.lock();
        keyPressed.wait(&amp;amp;mutex);
        ++count;
        mutex.unlock();

        do_something();

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

        mutex.lock();
        &lt;span class=&quot;comment&quot;&gt;// Sleep until there are no busy worker threads&lt;/span&gt;
        while (count &amp;gt; 0) {
            mutex.unlock();
            sleep(1);
            mutex.lock();
        }
        keyPressed.wakeAll();
        mutex.unlock();
    }&lt;/pre&gt;
&lt;p&gt;The mutex is necessary because the results of two threads attempting to change the value of the same variable simultaneously are unpredictable.&lt;/p&gt;
&lt;p&gt;Wait conditions are a powerful thread synchronization primitive. The Wait Conditions&lt;/tt&gt; example shows how to use &lt;a href=&quot;QWaitCondition.html#QWaitCondition()&quot;&gt;&lt;tt&gt;QWaitCondition&lt;/tt&gt;&lt;/a&gt; as an alternative to &lt;a href=&quot;QSemaphore.html&quot;&gt;&lt;tt&gt;QSemaphore&lt;/tt&gt;&lt;/a&gt; for controlling access to a circular buffer shared by a producer thread and a consumer thread.&lt;/p&gt;

@see &lt;a href=&quot;QMutex.html&quot;&gt;&lt;tt&gt;QMutex&lt;/tt&gt;&lt;/a&gt;
@see &lt;a href=&quot;QSemaphore.html&quot;&gt;&lt;tt&gt;QSemaphore&lt;/tt&gt;&lt;/a&gt;
@see &lt;tt&gt;QThread&lt;/tt&gt;
@see Wait Conditions Example&lt;/tt&gt; */">
    <method name="public QWaitCondition()" doc="/**
&lt;p&gt;Constructs a new wait condition object.&lt;/p&gt;
 */"/>
    <method name="public final boolean wait(com.trolltech.qt.core.QMutex mutex, int time)" doc="/**
&lt;p&gt;Releases the locked &lt;tt&gt;mutex&lt;/tt&gt; and wait on the wait condition. The &lt;tt&gt;mutex&lt;/tt&gt; must be initially locked by the calling thread. If &lt;tt&gt;mutex&lt;/tt&gt; is not in a locked state, this function returns immediately. If &lt;tt&gt;mutex&lt;/tt&gt; is a recursive mutex, this function returns immediately. The &lt;tt&gt;mutex&lt;/tt&gt; will be unlocked, and the calling thread will block until either of these conditions is met:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Another thread signals it using &lt;a href=&quot;QWaitCondition.html#wakeOne()&quot;&gt;&lt;tt&gt;wakeOne&lt;/tt&gt;&lt;/a&gt; or &lt;a href=&quot;QWaitCondition.html#wakeAll()&quot;&gt;&lt;tt&gt;wakeAll&lt;/tt&gt;&lt;/a&gt;. This function will return true in this case.&lt;/li&gt;
&lt;li&gt;&lt;tt&gt;time&lt;/tt&gt; milliseconds has elapsed. If &lt;tt&gt;time&lt;/tt&gt; is &lt;tt&gt;ULONG_MAX&lt;/tt&gt; (the default), then the wait will never timeout (the event must be signalled). This function will return false if the wait timed out.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;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.&lt;/p&gt;

@see &lt;a href=&quot;QWaitCondition.html#wakeOne()&quot;&gt;&lt;tt&gt;wakeOne&lt;/tt&gt;&lt;/a&gt;
@see &lt;a href=&quot;QWaitCondition.html#wakeAll()&quot;&gt;&lt;tt&gt;wakeAll&lt;/tt&gt;&lt;/a&gt; */"/>
    <method name="public final boolean wait(com.trolltech.qt.core.QMutex mutex)" doc="/**
&lt;p&gt;Equivalent to &lt;a href=&quot;QWaitCondition.html#wait(com.trolltech.qt.core.QMutex, int)&quot;&gt;wait&lt;/tt&gt;&lt;/a&gt;(&lt;tt&gt;mutex&lt;/tt&gt;, ULONG_MAX). */"/>
    <method name="public final void wakeAll()" doc="/**
&lt;p&gt;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.&lt;/p&gt;

@see &lt;a href=&quot;QWaitCondition.html#wakeOne()&quot;&gt;&lt;tt&gt;wakeOne&lt;/tt&gt;&lt;/a&gt; */"/>
    <method name="public final void wakeOne()" doc="/**
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;

@see &lt;a href=&quot;QWaitCondition.html#wakeAll()&quot;&gt;&lt;tt&gt;wakeAll&lt;/tt&gt;&lt;/a&gt; */"/>
</class>