~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/qwaitcondition.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/src/corelib/thread/qwaitcondition_unix.cpp -->
 
6
<head>
 
7
    <title>Qt 4.0: QWaitCondition Class Reference</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<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="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">QWaitCondition Class Reference</h1>
 
21
<p>The QWaitCondition class provides a condition variable for synchronizing threads. <a href="#details">More...</a></p>
 
22
<pre>#include &lt;QWaitCondition&gt;</pre><p>Part of the <a href="qtcore.html">QtCore</a> module.</p>
 
23
<p><b>Note:</b> All the functions in this class are <a href="threads.html#thread-safe">thread-safe</a>.</p>
 
24
<ul>
 
25
<li><a href="qwaitcondition-members.html">List of all members, including inherited members</a></li>
 
26
</ul>
 
27
<a name="public-functions"></a>
 
28
<h3>Public Functions</h3>
 
29
<ul>
 
30
<li><div class="fn"/><b><a href="qwaitcondition.html#QWaitCondition">QWaitCondition</a></b> ()</li>
 
31
<li><div class="fn"/><b><a href="qwaitcondition.html#dtor.QWaitCondition">~QWaitCondition</a></b> ()</li>
 
32
<li><div class="fn"/>bool <b><a href="qwaitcondition.html#wait">wait</a></b> ( QMutex * <i>mutex</i>, unsigned long <i>time</i> = ULONG_MAX )</li>
 
33
<li><div class="fn"/>void <b><a href="qwaitcondition.html#wakeAll">wakeAll</a></b> ()</li>
 
34
<li><div class="fn"/>void <b><a href="qwaitcondition.html#wakeOne">wakeOne</a></b> ()</li>
 
35
</ul>
 
36
<a name="details"></a>
 
37
<hr />
 
38
<h2>Detailed Description</h2>
 
39
<p>The QWaitCondition class provides a condition variable for synchronizing threads.</p>
 
40
<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 event or <a href="qwaitcondition.html#wakeAll">wakeAll</a>() to wake them all.</p>
 
41
<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>
 
42
<pre>&nbsp;   forever {
 
43
        keyPressed.wait();
 
44
        do_something();
 
45
    }</pre>
 
46
<p>Here, the <tt>keyPressed</tt> variable is a global variable of type QWaitCondition.</p>
 
47
<p>A fourth thread would read key presses and wake the other three threads up every time it receives one, like this:</p>
 
48
<pre>&nbsp;   forever {
 
49
        getchar();
 
50
        keyPressed.wakeAll();
 
51
    }</pre>
 
52
<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 solve 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>
 
53
<pre>&nbsp;   forever {
 
54
        keyPressed.wait();
 
55
 
 
56
        mutex.lock();
 
57
        ++count;
 
58
        mutex.unlock();
 
59
 
 
60
        do_something();
 
61
 
 
62
        mutex.lock();
 
63
        --count;
 
64
        mutex.unlock();
 
65
    }</pre>
 
66
<p>Here's the code for the fourth thread:</p>
 
67
<pre>&nbsp;   forever {
 
68
        getchar();
 
69
 
 
70
        mutex.lock();
 
71
        // Sleep until there are no busy worker threads
 
72
        while (count &gt; 0) {
 
73
            mutex.unlock();
 
74
            sleep(1);
 
75
            mutex.lock();
 
76
        }
 
77
        mutex.unlock();
 
78
        keyPressed.wakeAll();
 
79
    }</pre>
 
80
<p>The mutex is necessary because the results of two threads attempting to change the value of the same variable simultaneously are unpredictable.</p>
 
81
<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>
 
82
<p>See also <a href="qmutex.html">QMutex</a>, <a href="qsemaphore.html">QSemaphore</a>, and <a href="qthread.html">QThread</a>.</p>
 
83
<hr />
 
84
<h2>Member Function Documentation</h2>
 
85
<h3 class="fn"><a name="QWaitCondition"></a>QWaitCondition::QWaitCondition ()</h3>
 
86
<p>Constructs a new wait condition object.</p>
 
87
<h3 class="fn"><a name="dtor.QWaitCondition"></a>QWaitCondition::~QWaitCondition ()</h3>
 
88
<p>Destroys the wait condition object.</p>
 
89
<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>
 
90
<p>Releases the locked <i>mutex</i> and wait on the thread event object. 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>
 
91
<ul>
 
92
<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>
 
93
<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>
 
94
</ul>
 
95
<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>
 
96
<p>See also <a href="qwaitcondition.html#wakeOne">wakeOne</a>() and <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>
 
97
<h3 class="fn"><a name="wakeAll"></a>void QWaitCondition::wakeAll ()</h3>
 
98
<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>
 
99
<p>See also <a href="qwaitcondition.html#wakeOne">wakeOne</a>().</p>
 
100
<h3 class="fn"><a name="wakeOne"></a>void QWaitCondition::wakeOne ()</h3>
 
101
<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>
 
102
<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>
 
103
<p>See also <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>
 
104
<p /><address><hr /><div align="center">
 
105
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
106
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
107
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
108
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
109
</tr></table></div></address></body>
 
110
</html>