qwaitcondition.html 14.2 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qwaitcondition.qdoc -->
  <title>QWaitCondition Class | Qt Core 5.11</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtcore-index.html">Qt Core</a></td><td ><a href="qtcore-module.html">C++ Classes</a></td><td >QWaitCondition</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.11.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#public-functions">Public Functions</a></li>
<li class="level1"><a href="#details">Detailed Description</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QWaitCondition Class</h1>
<!-- $$$QWaitCondition-brief -->
<p>The <a href="qwaitcondition.html">QWaitCondition</a> class provides a condition variable for synchronizing threads. <a href="#details">More...</a></p>
<!-- @@@QWaitCondition -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign">   <span class="preprocessor">#include &lt;QWaitCondition&gt;</span>
</td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += core</td></tr></table></div><ul>
<li><a href="qwaitcondition-members.html">List of all members, including inherited members</a></li>
</ul>
<p><b>Note:</b> All functions in this class are <a href="../qtdoc/threads-reentrancy.html">thread-safe</a>.</p>
<a name="public-functions"></a>
<h2 id="public-functions">Public Functions</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qwaitcondition.html#QWaitCondition">QWaitCondition</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qwaitcondition.html#dtor.QWaitCondition">~QWaitCondition</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qwaitcondition.html#notify_all">notify_all</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qwaitcondition.html#notify_one">notify_one</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qwaitcondition.html#wait">wait</a></b>(QMutex *<i>lockedMutex</i>, unsigned long <i>time</i> = ULONG_MAX)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qwaitcondition.html#wait-1">wait</a></b>(QReadWriteLock *<i>lockedReadWriteLock</i>, unsigned long <i>time</i> = ULONG_MAX)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qwaitcondition.html#wakeAll">wakeAll</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qwaitcondition.html#wakeOne">wakeOne</a></b>()</td></tr>
</table></div>
<a name="details"></a>
<!-- $$$QWaitCondition-description -->
<div class="descr">
<h2 id="details">Detailed Description</h2>
<p>The <a href="qwaitcondition.html">QWaitCondition</a> class provides a condition variable for synchronizing threads.</p>
<p><a href="qwaitcondition.html">QWaitCondition</a> allows a thread to tell other threads that some sort of condition has been met. One or many threads can block waiting for a <a href="qwaitcondition.html">QWaitCondition</a> 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 thread 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 class="cpp">

  forever {
      mutex<span class="operator">.</span>lock();
      keyPressed<span class="operator">.</span>wait(<span class="operator">&amp;</span>mutex);
      do_something();
      mutex<span class="operator">.</span>unlock();
  }

</pre>
<p>Here, the <code>keyPressed</code> variable is a global variable of type <a href="qwaitcondition.html">QWaitCondition</a>.</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 class="cpp">

  forever {
      getchar();
      keyPressed<span class="operator">.</span>wakeAll();
  }

</pre>
<p>The order in which the three threads are woken up is undefined. Also, if some of the threads are still in <code>do_something()</code> 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 class="cpp">

  forever {
      mutex<span class="operator">.</span>lock();
      keyPressed<span class="operator">.</span>wait(<span class="operator">&amp;</span>mutex);
      <span class="operator">+</span><span class="operator">+</span>count;
      mutex<span class="operator">.</span>unlock();

      do_something();

      mutex<span class="operator">.</span>lock();
      <span class="operator">-</span><span class="operator">-</span>count;
      mutex<span class="operator">.</span>unlock();
  }

</pre>
<p>Here's the code for the fourth thread:</p>
<pre class="cpp">

  forever {
      getchar();

      mutex<span class="operator">.</span>lock();
      <span class="comment">// Sleep until there are no busy worker threads</span>
      <span class="keyword">while</span> (count <span class="operator">&gt;</span> <span class="number">0</span>) {
          mutex<span class="operator">.</span>unlock();
          sleep(<span class="number">1</span>);
          mutex<span class="operator">.</span>lock();
      }
      keyPressed<span class="operator">.</span>wakeAll();
      mutex<span class="operator">.</span>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="qtcore-threads-waitconditions-example.html">Wait Conditions Example</a> example shows how to use <a href="qwaitcondition.html">QWaitCondition</a> 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>
</div>
<p><b>See also </b><a href="qmutex.html">QMutex</a>, <a href="qsemaphore.html">QSemaphore</a>, <a href="qthread.html">QThread</a>, and <a href="qtcore-threads-waitconditions-example.html">Wait Conditions Example</a>.</p>
<!-- @@@QWaitCondition -->
<div class="func">
<h2>Member Function Documentation</h2>
<!-- $$$QWaitCondition[overload1]$$$QWaitCondition -->
<h3 class="fn" id="QWaitCondition"><a name="QWaitCondition"></a>QWaitCondition::<span class="name">QWaitCondition</span>()</h3>
<p>Constructs a new wait condition object.</p>
<!-- @@@QWaitCondition -->
<!-- $$$~QWaitCondition[overload1]$$$~QWaitCondition -->
<h3 class="fn" id="dtor.QWaitCondition"><a name="dtor.QWaitCondition"></a>QWaitCondition::<span class="name">~QWaitCondition</span>()</h3>
<p>Destroys the wait condition object.</p>
<!-- @@@~QWaitCondition -->
<!-- $$$notify_all[overload1]$$$notify_all -->
<h3 class="fn" id="notify_all"><a name="notify_all"></a><span class="type">void</span> QWaitCondition::<span class="name">notify_all</span>()</h3>
<p>This function is provided for STL compatibility. It is equivalent to <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>
<p>This function was introduced in  Qt 5.8.</p>
<!-- @@@notify_all -->
<!-- $$$notify_one[overload1]$$$notify_one -->
<h3 class="fn" id="notify_one"><a name="notify_one"></a><span class="type">void</span> QWaitCondition::<span class="name">notify_one</span>()</h3>
<p>This function is provided for STL compatibility. It is equivalent to <a href="qwaitcondition.html#wakeOne">wakeOne</a>().</p>
<p>This function was introduced in  Qt 5.8.</p>
<!-- @@@notify_one -->
<!-- $$$wait[overload1]$$$waitQMutex*unsignedlong -->
<h3 class="fn" id="wait"><a name="wait"></a><span class="type">bool</span> QWaitCondition::<span class="name">wait</span>(<span class="type"><a href="qmutex.html">QMutex</a></span> *<i>lockedMutex</i>, <span class="type">unsigned</span> <span class="type">long</span> <i>time</i> = ULONG_MAX)</h3>
<p>Releases the <i>lockedMutex</i> and waits on the wait condition. The <i>lockedMutex</i> must be initially locked by the calling thread. If <i>lockedMutex</i> is not in a locked state, the behavior is undefined. If <i>lockedMutex</i> is a recursive mutex, this function returns immediately. The <i>lockedMutex</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 <code>ULONG_MAX</code> (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>lockedMutex</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><b>See also </b><a href="qwaitcondition.html#wakeOne">wakeOne</a>() and <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>
<!-- @@@wait -->
<!-- $$$wait$$$waitQReadWriteLock*unsignedlong -->
<h3 class="fn" id="wait-1"><a name="wait-1"></a><span class="type">bool</span> QWaitCondition::<span class="name">wait</span>(<span class="type"><a href="qreadwritelock.html">QReadWriteLock</a></span> *<i>lockedReadWriteLock</i>, <span class="type">unsigned</span> <span class="type">long</span> <i>time</i> = ULONG_MAX)</h3>
<p>Releases the <i>lockedReadWriteLock</i> and waits on the wait condition. The <i>lockedReadWriteLock</i> must be initially locked by the calling thread. If <i>lockedReadWriteLock</i> is not in a locked state, this function returns immediately. The <i>lockedReadWriteLock</i> must not be locked recursively, otherwise this function will not release the lock properly. The <i>lockedReadWriteLock</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 <code>ULONG_MAX</code> (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>lockedReadWriteLock</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><b>See also </b><a href="qwaitcondition.html#wakeOne">wakeOne</a>() and <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>
<!-- @@@wait -->
<!-- $$$wakeAll[overload1]$$$wakeAll -->
<h3 class="fn" id="wakeAll"><a name="wakeAll"></a><span class="type">void</span> QWaitCondition::<span class="name">wakeAll</span>()</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><b>See also </b><a href="qwaitcondition.html#wakeOne">wakeOne</a>().</p>
<!-- @@@wakeAll -->
<!-- $$$wakeOne[overload1]$$$wakeOne -->
<h3 class="fn" id="wakeOne"><a name="wakeOne"></a><span class="type">void</span> QWaitCondition::<span class="name">wakeOne</span>()</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><b>See also </b><a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>
<!-- @@@wakeOne -->
</div>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2018 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>