Sophie

Sophie

distrib > Fedora > 14 > x86_64 > by-pkgid > 3f684444755c5ad1181cdd5a97348f3c > files > 1330

PyQt4-devel-4.7.4-2.fc14.i686.rpm

<?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>QMutexLocker 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">&#160;&#160;</td><td class="postheader" valign="center"><a href="../pyqt4ref.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QMutexLocker Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes. <a href="#details">More...</a></p>
<h3>Methods</h3><ul><li><div class="fn" /><b><a href="qmutexlocker.html#QMutexLocker">__init__</a></b> (<i>self</i>, QMutex)</li><li><div class="fn" />QMutex <b><a href="qmutexlocker.html#mutex">mutex</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qmutexlocker.html#relock">relock</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qmutexlocker.html#unlock">unlock</a></b> (<i>self</i>)</li></ul><h3>Special Methods</h3><ul><li><div class="fn" />object <b><a href="qmutexlocker.html#__enter__">__enter__</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qmutexlocker.html#__exit__">__exit__</a></b> (<i>self</i>, object&#160;<i>type</i>, object&#160;<i>value</i>, object&#160;<i>traceback</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes.</p>
<p>Locking and unlocking a <a href="qmutex.html">QMutex</a> in complex functions and statements or in exception handling code is error-prone and difficult to debug. QMutexLocker can be used in such situations to ensure that the state of the mutex is always well-defined.</p>
<p>QMutexLocker should be created within a function where a <a href="qmutex.html">QMutex</a> needs to be locked. The mutex is locked when QMutexLocker is created. You can unlock and relock the mutex with <tt>unlock()</tt> and <tt>relock()</tt>. If locked, the mutex will be unlocked when the QMutexLocker is destroyed.</p>
<p>For example, this complex function locks a <a href="qmutex.html">QMutex</a> upon entering the function and unlocks the mutex at all the exit points:</p>
<pre> int complexFunction(int flag)
 {
     mutex.lock();

     int retVal = 0;

     switch (flag) {
     case 0:
     case 1:
         mutex.unlock();
         return moreComplexFunction(flag);
     case 2:
         {
             int status = anotherFunction();
             if (status &lt; 0) {
                 mutex.unlock();
                 return -2;
             }
             retVal = status + flag;
         }
         break;
     default:
         if (flag &gt; 10) {
             mutex.unlock();
             return -1;
         }
         break;
     }

     mutex.unlock();
     return retVal;
 }</pre>
<p>This example function will get more complicated as it is developed, which increases the likelihood that errors will occur.</p>
<p>Using QMutexLocker greatly simplifies the code, and makes it more readable:</p>
<pre> int complexFunction(int flag)
 {
     QMutexLocker locker(&amp;mutex);

     int retVal = 0;

     switch (flag) {
     case 0:
     case 1:
         return moreComplexFunction(flag);
     case 2:
         {
             int status = anotherFunction();
             if (status &lt; 0)
                 return -2;
             retVal = status + flag;
         }
         break;
     default:
         if (flag &gt; 10)
             return -1;
         break;
     }

     return retVal;
 }</pre>
<p>Now, the mutex will always be unlocked when the QMutexLocker object is destroyed (when the function returns since <tt>locker</tt> is an auto variable).</p>
<p>The same principle applies to code that throws and catches exceptions. An exception that is not caught in the function that has locked the mutex has no way of unlocking the mutex before the exception is passed up the stack to the calling function.</p>
<p>QMutexLocker also provides a <tt>mutex()</tt> member function that returns the mutex on which the QMutexLocker is operating. This is useful for code that needs access to the mutex, such as <a href="qwaitcondition.html#wait">QWaitCondition.wait</a>(). For example:</p>
<pre> class SignalWaiter
 {
 private:
     QMutexLocker locker;

 public:
     SignalWaiter(QMutex *mutex)
         : locker(mutex)
     {
     }

     void waitForSignal()
     {
         ...
         while (!signalled)
             waitCondition.wait(locker.mutex());
         ...
     }
 };</pre>
<p>See also <a href="qreadlocker.html">QReadLocker</a>, <a href="qwritelocker.html">QWriteLocker</a>, and <a href="qmutex.html">QMutex</a>.</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QMutexLocker" />QMutexLocker.__init__ (<i>self</i>, <a href="qmutex.html">QMutex</a>)</h3><p>Constructs a <a href="qmutexlocker.html">QMutexLocker</a> and locks <i>mutex</i>. The mutex will be unlocked when the <a href="qmutexlocker.html">QMutexLocker</a> is destroyed. If <i>mutex</i> is zero, <a href="qmutexlocker.html">QMutexLocker</a> does nothing.</p>
<p>See also <a href="qmutex.html#lock">QMutex.lock</a>().</p>
<a name="//apple_ref/cpp/instm/QMutexLocker/~QMutexLocker" />
<h3 class="fn"><a name="mutex" /><a href="qmutex.html">QMutex</a> QMutexLocker.mutex (<i>self</i>)</h3><p>Returns a pointer to the mutex that was locked in the constructor.</p>
<a name="//apple_ref/cpp/instm/QMutexLocker/relock" />
<h3 class="fn"><a name="relock" />QMutexLocker.relock (<i>self</i>)</h3><p>Relocks an unlocked mutex locker.</p>
<p>See also <a href="qmutexlocker.html#unlock">unlock</a>().</p>
<a name="//apple_ref/cpp/instm/QMutexLocker/unlock" />
<h3 class="fn"><a name="unlock" />QMutexLocker.unlock (<i>self</i>)</h3><p>Unlocks this mutex locker. You can use <tt>relock()</tt> to lock it again. It does not need to be locked when destroyed.</p>
<p>See also <a href="qmutexlocker.html#relock">relock</a>().</p>
<p /><h3 class="fn"><a name="__enter__" />object QMutexLocker.__enter__ (<i>self</i>)</h3><h3 class="fn"><a name="__exit__" />QMutexLocker.__exit__ (<i>self</i>, object&#160;<i>type</i>, object&#160;<i>value</i>, object&#160;<i>traceback</i>)</h3><address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;4.7.4 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2010</td><td align="right" width="25%">Qt&#160;4.6.3</td></tr></table></div></address></body></html>