<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html><head><title>QReadWriteLock Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } td.postheader { font-family: sans-serif } tr.address { font-family: sans-serif } body { background: #ffffff; color: black; } </style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">  </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QReadWriteLock Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QReadWriteLock class provides read-write locking. <a href="#details">More...</a></p> <h3>Types</h3><ul><li><div class="fn" />enum <b><a href="qreadwritelock.html#RecursionMode-enum">RecursionMode</a></b> { NonRecursive, Recursive }</li></ul><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qreadwritelock.html#QReadWriteLock">__init__</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qreadwritelock.html#QReadWriteLock-2">__init__</a></b> (<i>self</i>, RecursionMode <i>recursionMode</i>)</li><li><div class="fn" /><b><a href="qreadwritelock.html#lockForRead">lockForRead</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qreadwritelock.html#lockForWrite">lockForWrite</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qreadwritelock.html#tryLockForRead">tryLockForRead</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qreadwritelock.html#tryLockForRead-2">tryLockForRead</a></b> (<i>self</i>, int <i>timeout</i>)</li><li><div class="fn" />bool <b><a href="qreadwritelock.html#tryLockForWrite">tryLockForWrite</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qreadwritelock.html#tryLockForWrite-2">tryLockForWrite</a></b> (<i>self</i>, int <i>timeout</i>)</li><li><div class="fn" /><b><a href="qreadwritelock.html#unlock">unlock</a></b> (<i>self</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QReadWriteLock 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, QReadWriteLock is a direct competitor to <a href="qmutex.html">QMutex</a>. QReadWriteLock is a good choice if there are many concurrent reads and writing occurs infrequently.</p> <p>Example:</p> <pre class="cpp"> <span class="type">QReadWriteLock</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 QReadWriteLock can be recursively locked by the same thread when constructed in <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. it is not possible to lock for reading in a thread that already has locked for writing (and vice versa).</p> <hr /><h2>Type Documentation</h2><h3 class="fn"><a name="RecursionMode-enum" />QReadWriteLock.RecursionMode</h3><table class="valuelist"> <tr class="odd" valign="top"> <th class="tblConst">Constant</th> <th class="tblval">Value</th> <th class="tbldscr">Description</th> </tr> <tr> <td class="topAlign"><tt>QReadWriteLock.Recursive</tt></td> <td class="topAlign"><tt>1</tt></td> <td class="topAlign">In this mode, a thread can lock the same <a href="qreadwritelock.html">QReadWriteLock</a> multiple times and the mutex 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"><tt>QReadWriteLock.NonRecursive</tt></td> <td class="topAlign"><tt>0</tt></td> <td class="topAlign">In this mode, a thread may only lock a <a href="qreadwritelock.html">QReadWriteLock</a> once.</td> </tr> </table> <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> <hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QReadWriteLock" />QReadWriteLock.__init__ (<i>self</i>)</h3><p>Constructs a <a href="qreadwritelock.html">QReadWriteLock</a> object in <a href="qreadwritelock.html#RecursionMode-enum">NonRecursive</a> mode.</p> <p><b>See also</b> <a href="qreadwritelock.html#lockForRead">lockForRead</a>() and <a href="qreadwritelock.html#lockForWrite">lockForWrite</a>().</p> <h3 class="fn"><a name="QReadWriteLock-2" />QReadWriteLock.__init__ (<i>self</i>, <a href="qreadwritelock.html#RecursionMode-enum">RecursionMode</a> <i>recursionMode</i>)</h3><p>Constructs a <a href="qreadwritelock.html">QReadWriteLock</a> object in the given <i>recursionMode</i>.</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> <h3 class="fn"><a name="lockForRead" />QReadWriteLock.lockForRead (<i>self</i>)</h3><p>Locks the lock for reading. This function will block the current thread if any thread (including the current) has locked for writing.</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> <h3 class="fn"><a name="lockForWrite" />QReadWriteLock.lockForWrite (<i>self</i>)</h3><p>Locks the lock for writing. This function will block the current thread if another thread has locked for reading or writing.</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> <h3 class="fn"><a name="tryLockForRead" />bool QReadWriteLock.tryLockForRead (<i>self</i>)</h3><p>Attempts to lock for reading. If the lock was obtained, this function returns true, otherwise it returns false instead of waiting for the lock to become available, i.e. 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.</p> <p><b>See also</b> <a href="qreadwritelock.html#unlock">unlock</a>() and <a href="qreadwritelock.html#lockForRead">lockForRead</a>().</p> <h3 class="fn"><a name="tryLockForRead-2" />bool QReadWriteLock.tryLockForRead (<i>self</i>, int <i>timeout</i>)</h3><p>This is an overloaded function.</p> <p>Attempts to lock for reading. This function returns true if the lock was obtained; otherwise it returns false. 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. 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.</p> <p><b>See also</b> <a href="qreadwritelock.html#unlock">unlock</a>() and <a href="qreadwritelock.html#lockForRead">lockForRead</a>().</p> <h3 class="fn"><a name="tryLockForWrite" />bool QReadWriteLock.tryLockForWrite (<i>self</i>)</h3><p>Attempts to lock for writing. If the lock was obtained, this function returns true; otherwise, it returns false 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><b>See also</b> <a href="qreadwritelock.html#unlock">unlock</a>() and <a href="qreadwritelock.html#lockForWrite">lockForWrite</a>().</p> <h3 class="fn"><a name="tryLockForWrite-2" />bool QReadWriteLock.tryLockForWrite (<i>self</i>, int <i>timeout</i>)</h3><p>This is an overloaded function.</p> <p>Attempts to lock for writing. This function returns true if the lock was obtained; otherwise it returns false. 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. 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><b>See also</b> <a href="qreadwritelock.html#unlock">unlock</a>() and <a href="qreadwritelock.html#lockForWrite">lockForWrite</a>().</p> <h3 class="fn"><a name="unlock" />QReadWriteLock.unlock (<i>self</i>)</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> <address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.10.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt 4.8.5</td></tr></table></div></address></body></html>