Sophie

Sophie

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

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" />
<!-- qreadwritelock.cpp -->
  <title>QReadWriteLock 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 >QReadWriteLock</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-types">Public Types</a></li>
<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">QReadWriteLock Class</h1>
<!-- $$$QReadWriteLock-brief -->
<p>The <a href="qreadwritelock.html">QReadWriteLock</a> class provides read-write locking. <a href="#details">More...</a></p>
<!-- @@@QReadWriteLock -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign">   <span class="preprocessor">#include &lt;QReadWriteLock&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="qreadwritelock-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-types"></a>
<h2 id="public-types">Public Types</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> enum </td><td class="memItemRight bottomAlign"><b><a href="qreadwritelock.html#RecursionMode-enum">RecursionMode</a></b> { Recursive, NonRecursive }</td></tr>
</table></div>
<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="qreadwritelock.html#QReadWriteLock">QReadWriteLock</a></b>(RecursionMode <i>recursionMode</i> = NonRecursive)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qreadwritelock.html#dtor.QReadWriteLock">~QReadWriteLock</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qreadwritelock.html#lockForRead">lockForRead</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qreadwritelock.html#lockForWrite">lockForWrite</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qreadwritelock.html#tryLockForRead">tryLockForRead</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qreadwritelock.html#tryLockForRead-1">tryLockForRead</a></b>(int <i>timeout</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qreadwritelock.html#tryLockForWrite">tryLockForWrite</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qreadwritelock.html#tryLockForWrite-1">tryLockForWrite</a></b>(int <i>timeout</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qreadwritelock.html#unlock">unlock</a></b>()</td></tr>
</table></div>
<a name="details"></a>
<!-- $$$QReadWriteLock-description -->
<div class="descr">
<h2 id="details">Detailed Description</h2>
<p>The <a href="qreadwritelock.html">QReadWriteLock</a> class provides read-write locking.</p>
<p>A read-write lock is a synchronization tool for protecting resources that can be accessed for reading and writing. This type of lock is useful if you want to allow multiple threads to have simultaneous read-only access, but as soon as one thread wants to write to the resource, all other threads must be blocked until the writing is complete.</p>
<p>In many cases, <a href="qreadwritelock.html">QReadWriteLock</a> is a direct competitor to <a href="qmutex.html">QMutex</a>. <a href="qreadwritelock.html">QReadWriteLock</a> is a good choice if there are many concurrent reads and writing occurs infrequently.</p>
<p>Example:</p>
<pre class="cpp">

  <span class="type"><a href="qreadwritelock.html#QReadWriteLock">QReadWriteLock</a></span> lock;

  <span class="type">void</span> ReaderThread<span class="operator">::</span>run()
  {
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      lock<span class="operator">.</span>lockForRead();
      read_file();
      lock<span class="operator">.</span>unlock();
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
  }

  <span class="type">void</span> WriterThread<span class="operator">::</span>run()
  {
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      lock<span class="operator">.</span>lockForWrite();
      write_file();
      lock<span class="operator">.</span>unlock();
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
  }

</pre>
<p>To ensure that writers aren't blocked forever by readers, readers attempting to obtain a lock will not succeed if there is a blocked writer waiting for access, even if the lock is currently only accessed by other readers. Also, if the lock is accessed by a writer and another writer comes in, that writer will have priority over any readers that might also be waiting.</p>
<p>Like <a href="qmutex.html">QMutex</a>, a <a href="qreadwritelock.html">QReadWriteLock</a> can be recursively locked by the same thread when constructed with <a href="qreadwritelock.html#RecursionMode-enum">QReadWriteLock::Recursive</a> as <a href="qreadwritelock.html#RecursionMode-enum">QReadWriteLock::RecursionMode</a>. In such cases, <a href="qreadwritelock.html#unlock">unlock</a>() must be called the same number of times <a href="qreadwritelock.html#lockForWrite">lockForWrite</a>() or <a href="qreadwritelock.html#lockForRead">lockForRead</a>() was called. Note that the lock type cannot be changed when trying to lock recursively, i.e&#x2e; it is not possible to lock for reading in a thread that already has locked for writing (and vice versa).</p>
</div>
<p><b>See also </b><a href="qreadlocker.html">QReadLocker</a>, <a href="qwritelocker.html">QWriteLocker</a>, <a href="qmutex.html">QMutex</a>, and <a href="qsemaphore.html">QSemaphore</a>.</p>
<!-- @@@QReadWriteLock -->
<div class="types">
<h2>Member Type Documentation</h2>
<!-- $$$RecursionMode$$$NonRecursive$$$Recursive -->
<h3 class="fn" id="RecursionMode-enum"><a name="RecursionMode-enum"></a>enum QReadWriteLock::<span class="name">RecursionMode</span></h3>
<div class="table"><table class="valuelist"><tr valign="top" class="odd"><th class="tblConst">Constant</th><th class="tblval">Value</th><th class="tbldscr">Description</th></tr>
<tr><td class="topAlign"><code>QReadWriteLock::Recursive</code></td><td class="topAlign tblval"><code>1</code></td><td class="topAlign">In this mode, a thread can lock the same <a href="qreadwritelock.html">QReadWriteLock</a> multiple times. The <a href="qreadwritelock.html">QReadWriteLock</a> won't be unlocked until a corresponding number of <a href="qreadwritelock.html#unlock">unlock</a>() calls have been made.</td></tr>
<tr><td class="topAlign"><code>QReadWriteLock::NonRecursive</code></td><td class="topAlign tblval"><code>0</code></td><td class="topAlign">In this mode, a thread may only lock a <a href="qreadwritelock.html">QReadWriteLock</a> once.</td></tr>
</table></div>
<p>This enum was introduced or modified in  Qt 4.4.</p>
<p><b>See also </b><a href="qreadwritelock.html#QReadWriteLock">QReadWriteLock</a>().</p>
<!-- @@@RecursionMode -->
</div>
<div class="func">
<h2>Member Function Documentation</h2>
<!-- $$$QReadWriteLock[overload1]$$$QReadWriteLockRecursionMode -->
<h3 class="fn" id="QReadWriteLock"><a name="QReadWriteLock"></a>QReadWriteLock::<span class="name">QReadWriteLock</span>(<span class="type"><a href="qreadwritelock.html#RecursionMode-enum">RecursionMode</a></span> <i>recursionMode</i> = NonRecursive)</h3>
<p>Constructs a <a href="qreadwritelock.html">QReadWriteLock</a> object in the given <i>recursionMode</i>.</p>
<p>The default recursion mode is <a href="qreadwritelock.html#RecursionMode-enum">NonRecursive</a>.</p>
<p>This function was introduced in  Qt 4.4.</p>
<p><b>See also </b><a href="qreadwritelock.html#lockForRead">lockForRead</a>(), <a href="qreadwritelock.html#lockForWrite">lockForWrite</a>(), and <a href="qreadwritelock.html#RecursionMode-enum">RecursionMode</a>.</p>
<!-- @@@QReadWriteLock -->
<!-- $$$~QReadWriteLock[overload1]$$$~QReadWriteLock -->
<h3 class="fn" id="dtor.QReadWriteLock"><a name="dtor.QReadWriteLock"></a>QReadWriteLock::<span class="name">~QReadWriteLock</span>()</h3>
<p>Destroys the <a href="qreadwritelock.html">QReadWriteLock</a> object.</p>
<p><b>Warning:</b> Destroying a read-write lock that is in use may result in undefined behavior.</p>
<!-- @@@~QReadWriteLock -->
<!-- $$$lockForRead[overload1]$$$lockForRead -->
<h3 class="fn" id="lockForRead"><a name="lockForRead"></a><span class="type">void</span> QReadWriteLock::<span class="name">lockForRead</span>()</h3>
<p>Locks the lock for reading. This function will block the current thread if another thread has locked for writing.</p>
<p>It is not possible to lock for read if the thread already has locked for write.</p>
<p><b>See also </b><a href="qreadwritelock.html#unlock">unlock</a>(), <a href="qreadwritelock.html#lockForWrite">lockForWrite</a>(), and <a href="qreadwritelock.html#tryLockForRead">tryLockForRead</a>().</p>
<!-- @@@lockForRead -->
<!-- $$$lockForWrite[overload1]$$$lockForWrite -->
<h3 class="fn" id="lockForWrite"><a name="lockForWrite"></a><span class="type">void</span> QReadWriteLock::<span class="name">lockForWrite</span>()</h3>
<p>Locks the lock for writing. This function will block the current thread if another thread (including the current) has locked for reading or writing (unless the lock has been created using the <a href="qreadwritelock.html#RecursionMode-enum">QReadWriteLock::Recursive</a> mode).</p>
<p>It is not possible to lock for write if the thread already has locked for read.</p>
<p><b>See also </b><a href="qreadwritelock.html#unlock">unlock</a>(), <a href="qreadwritelock.html#lockForRead">lockForRead</a>(), and <a href="qreadwritelock.html#tryLockForWrite">tryLockForWrite</a>().</p>
<!-- @@@lockForWrite -->
<!-- $$$tryLockForRead[overload1]$$$tryLockForRead -->
<h3 class="fn" id="tryLockForRead"><a name="tryLockForRead"></a><span class="type">bool</span> QReadWriteLock::<span class="name">tryLockForRead</span>()</h3>
<p>Attempts to lock for reading. If the lock was obtained, this function returns <code>true</code>, otherwise it returns <code>false</code> instead of waiting for the lock to become available, i.e&#x2e; it does not block.</p>
<p>The lock attempt will fail if another thread has locked for writing.</p>
<p>If the lock was obtained, the lock must be unlocked with <a href="qreadwritelock.html#unlock">unlock</a>() before another thread can successfully lock it for writing.</p>
<p>It is not possible to lock for read if the thread already has locked for write.</p>
<p><b>See also </b><a href="qreadwritelock.html#unlock">unlock</a>() and <a href="qreadwritelock.html#lockForRead">lockForRead</a>().</p>
<!-- @@@tryLockForRead -->
<!-- $$$tryLockForRead$$$tryLockForReadint -->
<h3 class="fn" id="tryLockForRead-1"><a name="tryLockForRead-1"></a><span class="type">bool</span> QReadWriteLock::<span class="name">tryLockForRead</span>(<span class="type">int</span> <i>timeout</i>)</h3>
<p>This is an overloaded function.</p>
<p>Attempts to lock for reading. This function returns <code>true</code> if the lock was obtained; otherwise it returns <code>false</code>. If another thread has locked for writing, this function will wait for at most <i>timeout</i> milliseconds for the lock to become available.</p>
<p>Note: Passing a negative number as the <i>timeout</i> is equivalent to calling <a href="qreadwritelock.html#lockForRead">lockForRead</a>(), i.e&#x2e; this function will wait forever until lock can be locked for reading when <i>timeout</i> is negative.</p>
<p>If the lock was obtained, the lock must be unlocked with <a href="qreadwritelock.html#unlock">unlock</a>() before another thread can successfully lock it for writing.</p>
<p>It is not possible to lock for read if the thread already has locked for write.</p>
<p><b>See also </b><a href="qreadwritelock.html#unlock">unlock</a>() and <a href="qreadwritelock.html#lockForRead">lockForRead</a>().</p>
<!-- @@@tryLockForRead -->
<!-- $$$tryLockForWrite[overload1]$$$tryLockForWrite -->
<h3 class="fn" id="tryLockForWrite"><a name="tryLockForWrite"></a><span class="type">bool</span> QReadWriteLock::<span class="name">tryLockForWrite</span>()</h3>
<p>Attempts to lock for writing. If the lock was obtained, this function returns <code>true</code>; otherwise, it returns <code>false</code> immediately.</p>
<p>The lock attempt will fail if another thread has locked for reading or writing.</p>
<p>If the lock was obtained, the lock must be unlocked with <a href="qreadwritelock.html#unlock">unlock</a>() before another thread can successfully lock it.</p>
<p>It is not possible to lock for write if the thread already has locked for read.</p>
<p><b>See also </b><a href="qreadwritelock.html#unlock">unlock</a>() and <a href="qreadwritelock.html#lockForWrite">lockForWrite</a>().</p>
<!-- @@@tryLockForWrite -->
<!-- $$$tryLockForWrite$$$tryLockForWriteint -->
<h3 class="fn" id="tryLockForWrite-1"><a name="tryLockForWrite-1"></a><span class="type">bool</span> QReadWriteLock::<span class="name">tryLockForWrite</span>(<span class="type">int</span> <i>timeout</i>)</h3>
<p>This is an overloaded function.</p>
<p>Attempts to lock for writing. This function returns <code>true</code> if the lock was obtained; otherwise it returns <code>false</code>. If another thread has locked for reading or writing, this function will wait for at most <i>timeout</i> milliseconds for the lock to become available.</p>
<p>Note: Passing a negative number as the <i>timeout</i> is equivalent to calling <a href="qreadwritelock.html#lockForWrite">lockForWrite</a>(), i.e&#x2e; this function will wait forever until lock can be locked for writing when <i>timeout</i> is negative.</p>
<p>If the lock was obtained, the lock must be unlocked with <a href="qreadwritelock.html#unlock">unlock</a>() before another thread can successfully lock it.</p>
<p>It is not possible to lock for write if the thread already has locked for read.</p>
<p><b>See also </b><a href="qreadwritelock.html#unlock">unlock</a>() and <a href="qreadwritelock.html#lockForWrite">lockForWrite</a>().</p>
<!-- @@@tryLockForWrite -->
<!-- $$$unlock[overload1]$$$unlock -->
<h3 class="fn" id="unlock"><a name="unlock"></a><span class="type">void</span> QReadWriteLock::<span class="name">unlock</span>()</h3>
<p>Unlocks the lock.</p>
<p>Attempting to unlock a lock that is not locked is an error, and will result in program termination.</p>
<p><b>See also </b><a href="qreadwritelock.html#lockForRead">lockForRead</a>(), <a href="qreadwritelock.html#lockForWrite">lockForWrite</a>(), <a href="qreadwritelock.html#tryLockForRead">tryLockForRead</a>(), and <a href="qreadwritelock.html#tryLockForWrite">tryLockForWrite</a>().</p>
<!-- @@@unlock -->
</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>