Sophie

Sophie

distrib > Mageia > 6 > armv5tl > media > core-updates > by-pkgid > 768f7d9f703884aa2562bf0a651086df > files > 861

qtbase5-doc-5.9.4-1.1.mga6.noarch.rpm

<?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.9</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 >Qt 5.9</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.9.4 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 thread-safe.</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> 2017 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>