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
|
<?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">  </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">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 <i>m</i>)</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 <i>type</i>, object <i>value</i>, object <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 class="cpp">
<span class="type">int</span> complexFunction(<span class="type">int</span> flag)
{
mutex<span class="operator">.</span>lock();
<span class="type">int</span> retVal <span class="operator">=</span> <span class="number">0</span>;
<span class="keyword">switch</span> (flag) {
<span class="keyword">case</span> <span class="number">0</span>:
<span class="keyword">case</span> <span class="number">1</span>:
retVal <span class="operator">=</span> moreComplexFunction(flag);
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="number">2</span>:
{
<span class="type">int</span> status <span class="operator">=</span> anotherFunction();
<span class="keyword">if</span> (status <span class="operator"><</span> <span class="number">0</span>) {
mutex<span class="operator">.</span>unlock();
<span class="keyword">return</span> <span class="operator">-</span><span class="number">2</span>;
}
retVal <span class="operator">=</span> status <span class="operator">+</span> flag;
}
<span class="keyword">break</span>;
<span class="keyword">default</span>:
<span class="keyword">if</span> (flag <span class="operator">></span> <span class="number">10</span>) {
mutex<span class="operator">.</span>unlock();
<span class="keyword">return</span> <span class="operator">-</span><span class="number">1</span>;
}
<span class="keyword">break</span>;
}
mutex<span class="operator">.</span>unlock();
<span class="keyword">return</span> 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 class="cpp">
<span class="type">int</span> complexFunction(<span class="type">int</span> flag)
{
<span class="type">QMutexLocker</span> locker(<span class="operator">&</span>mutex);
<span class="type">int</span> retVal <span class="operator">=</span> <span class="number">0</span>;
<span class="keyword">switch</span> (flag) {
<span class="keyword">case</span> <span class="number">0</span>:
<span class="keyword">case</span> <span class="number">1</span>:
<span class="keyword">return</span> moreComplexFunction(flag);
<span class="keyword">case</span> <span class="number">2</span>:
{
<span class="type">int</span> status <span class="operator">=</span> anotherFunction();
<span class="keyword">if</span> (status <span class="operator"><</span> <span class="number">0</span>)
<span class="keyword">return</span> <span class="operator">-</span><span class="number">2</span>;
retVal <span class="operator">=</span> status <span class="operator">+</span> flag;
}
<span class="keyword">break</span>;
<span class="keyword">default</span>:
<span class="keyword">if</span> (flag <span class="operator">></span> <span class="number">10</span>)
<span class="keyword">return</span> <span class="operator">-</span><span class="number">1</span>;
<span class="keyword">break</span>;
}
<span class="keyword">return</span> 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="cpp">
<span class="keyword">class</span> SignalWaiter
{
<span class="keyword">private</span>:
<span class="type">QMutexLocker</span> locker;
<span class="keyword">public</span>:
SignalWaiter(<span class="type"><a href="qmutex.html">QMutex</a></span> <span class="operator">*</span>mutex)
: locker(mutex)
{
}
<span class="type">void</span> waitForSignal()
{
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
<span class="keyword">while</span> (<span class="operator">!</span>signalled)
waitCondition<span class="operator">.</span>wait(locker<span class="operator">.</span>mutex());
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
}
};
</pre><hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QMutexLocker" />QMutexLocker.__init__ (<i>self</i>, <a href="qmutex.html">QMutex</a> <i>m</i>)</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><b>See also</b> <a href="qmutex.html#lock">QMutex.lock</a>().</p>
<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>
<h3 class="fn"><a name="relock" />QMutexLocker.relock (<i>self</i>)</h3><p>Relocks an unlocked mutex locker.</p>
<p><b>See also</b> <a href="qmutexlocker.html#unlock">unlock</a>().</p>
<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><b>See also</b> <a href="qmutexlocker.html#relock">relock</a>().</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 <i>type</i>, object <i>value</i>, object <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 4.12.1 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qt.io">The Qt Company</a> 2015</td><td align="right" width="25%">Qt 4.8.7</td></tr></table></div></address></body></html>
|