Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 4558

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- qmutex.cpp -->
<head>
  <title>Qt 4.6: QMutex Class Reference</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<a name="//apple_ref/cpp/cl//QMutex"></a>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">QMutex Class Reference<br /><span class="small-subtitle">[<a href="qtcore.html">QtCore</a> module]</span>
</h1>
<p>The QMutex class provides access serialization between threads. <a href="#details">More...</a></p>
<pre> #include &lt;QMutex&gt;</pre><p><b>Note:</b> All functions in this class are <a href="threads-reentrancy.html#thread-safe">thread-safe</a>.</p>
<ul>
<li><a href="qmutex-members.html">List of all members, including inherited members</a></li>
<li><a href="qmutex-qt3.html">Qt 3 support members</a></li>
</ul>
<hr />
<a name="public-types"></a>
<h2>Public Types</h2>
<table class="alignedsummary" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td class="memItemLeft" align="right" valign="top">enum </td><td class="memItemRight" valign="bottom"><b><a href="qmutex.html#RecursionMode-enum">RecursionMode</a></b> { Recursive, NonRecursive }</td></tr>
</table>
<hr />
<a name="public-functions"></a>
<h2>Public Functions</h2>
<table class="alignedsummary" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td class="memItemLeft" align="right" valign="top"></td><td class="memItemRight" valign="bottom"><b><a href="qmutex.html#QMutex">QMutex</a></b> ( RecursionMode <i>mode</i> = NonRecursive )</td></tr>
<tr><td class="memItemLeft" align="right" valign="top"></td><td class="memItemRight" valign="bottom"><b><a href="qmutex.html#dtor.QMutex">~QMutex</a></b> ()</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><b><a href="qmutex.html#lock">lock</a></b> ()</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><b><a href="qmutex.html#tryLock">tryLock</a></b> ()</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><b><a href="qmutex.html#tryLock-2">tryLock</a></b> ( int <i>timeout</i> )</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><b><a href="qmutex.html#unlock">unlock</a></b> ()</td></tr>
</table>
<a name="details"></a>
<hr />
<h2>Detailed Description</h2>
<p>The QMutex class provides access serialization between threads.</p>
<p>The purpose of a QMutex is to protect an object, data structure or section of code so that only one thread can access it at a time (this is similar to the Java <tt>synchronized</tt> keyword). It is usually best to use a mutex with a <a href="qmutexlocker.html">QMutexLocker</a> since this makes it easy to ensure that locking and unlocking are performed consistently.</p>
<p>For example, say there is a method that prints a message to the user on two lines:</p>
<pre> int number = 6;

 void method1()
 {
     number *= 5;
     number /= 4;
 }

 void method2()
 {
     number *= 3;
     number /= 2;
 }</pre>
<p>If these two methods are called in succession, the following happens:</p>
<pre><span class="comment"> // method1()</span>
 number *= 5;        <span class="comment">// number is now 30</span>
 number /= 4;        <span class="comment">// number is now 7</span>

<span class="comment"> // method2()</span>
 number *= 3;        <span class="comment">// number is now 21</span>
 number /= 2;        <span class="comment">// number is now 10</span></pre>
<p>If these two methods are called simultaneously from two threads then the following sequence could result:</p>
<pre><span class="comment"> // Thread 1 calls method1()</span>
 number *= 5;        <span class="comment">// number is now 30</span>

<span class="comment"> // Thread 2 calls method2().</span>
<span class="comment"> //</span>
<span class="comment"> // Most likely Thread 1 has been put to sleep by the operating</span>
<span class="comment"> // system to allow Thread 2 to run.</span>
 number *= 3;        <span class="comment">// number is now 90</span>
 number /= 2;        <span class="comment">// number is now 45</span>

<span class="comment"> // Thread 1 finishes executing.</span>
 number /= 4;        <span class="comment">// number is now 11, instead of 10</span></pre>
<p>If we add a mutex, we should get the result we want:</p>
<pre> QMutex mutex;
 int number = 6;

 void method1()
 {
     mutex.lock();
     number *= 5;
     number /= 4;
     mutex.unlock();
 }

 void method2()
 {
     mutex.lock();
     number *= 3;
     number /= 2;
     mutex.unlock();
 }</pre>
<p>Then only one thread can modify <tt>number</tt> at any given time and the result is correct. This is a trivial example, of course, but applies to any other case where things need to happen in a particular sequence.</p>
<p>When you call <a href="qmutex.html#lock">lock</a>() in a thread, other threads that try to call <a href="qmutex.html#lock">lock</a>() in the same place will block until the thread that got the lock calls <a href="qmutex.html#unlock">unlock</a>(). A non-blocking alternative to <a href="qmutex.html#lock">lock</a>() is <a href="qmutex.html#tryLock">tryLock</a>().</p>
<p>See also <a href="qmutexlocker.html">QMutexLocker</a>, <a href="qreadwritelock.html">QReadWriteLock</a>, <a href="qsemaphore.html">QSemaphore</a>, and <a href="qwaitcondition.html">QWaitCondition</a>.</p>
<hr />
<h2>Member Type Documentation</h2>
<a name="//apple_ref/cpp/tag/QMutex/RecursionMode"></a>
<a name="//apple_ref/cpp/econst/QMutex/Recursive"></a>
<a name="//apple_ref/cpp/econst/QMutex/NonRecursive"></a>
<h3 class="fn"><a name="RecursionMode-enum"></a>enum QMutex::RecursionMode</h3>
<p><table class="valuelist" border="1" cellpadding="2" cellspacing="1" width="100%">
<tr><th width="25%">Constant</th><th width="15%">Value</th><th width="60%">Description</th></tr>
<tr><td valign="top"><tt>QMutex::Recursive</tt></td><td align="center" valign="top"><tt>1</tt></td><td valign="top">In this mode, a thread can lock the same mutex multiple times and the mutex won't be unlocked until a corresponding number of <a href="qmutex.html#unlock">unlock</a>() calls have been made.</td></tr>
<tr><td valign="top"><tt>QMutex::NonRecursive</tt></td><td align="center" valign="top"><tt>0</tt></td><td valign="top">In this mode, a thread may only lock a mutex once.</td></tr>
</table></p>
<p>See also <a href="qmutex.html#QMutex">QMutex</a>().</p>
<hr />
<h2>Member Function Documentation</h2>
<a name="//apple_ref/cpp/instm/QMutex/QMutex"></a>
<h3 class="fn"><a name="QMutex"></a>QMutex::QMutex ( <a href="qmutex.html#RecursionMode-enum">RecursionMode</a> <i>mode</i> = NonRecursive )</h3>
<p>Constructs a new mutex. The mutex is created in an unlocked state.</p>
<p>If <i>mode</i> is <a href="qmutex.html#RecursionMode-enum">QMutex::Recursive</a>, a thread can lock the same mutex multiple times and the mutex won't be unlocked until a corresponding number of <a href="qmutex.html#unlock">unlock</a>() calls have been made. The default is <a href="qmutex.html#RecursionMode-enum">QMutex::NonRecursive</a>.</p>
<p>See also <a href="qmutex.html#lock">lock</a>() and <a href="qmutex.html#unlock">unlock</a>().</p>
<a name="//apple_ref/cpp/instm/QMutex/~QMutex"></a>
<h3 class="fn"><a name="dtor.QMutex"></a>QMutex::~QMutex ()</h3>
<p>Destroys the mutex.</p>
<p><b>Warning:</b> Destroying a locked mutex may result in undefined behavior.</p>
<a name="//apple_ref/cpp/instm/QMutex/lock"></a>
<h3 class="fn"><a name="lock"></a>void QMutex::lock ()</h3>
<p>Locks the mutex. If another thread has locked the mutex then this call will block until that thread has unlocked it.</p>
<p>Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a <a href="qmutex.html#RecursionMode-enum">recursive mutex</a>. If this mutex is a <a href="qmutex.html#RecursionMode-enum">non-recursive mutex</a>, this function will <i>dead-lock</i> when the mutex is locked recursively.</p>
<p>See also <a href="qmutex.html#unlock">unlock</a>().</p>
<a name="//apple_ref/cpp/instm/QMutex/tryLock"></a>
<h3 class="fn"><a name="tryLock"></a>bool QMutex::tryLock ()</h3>
<p>Attempts to lock the mutex. If the lock was obtained, this function returns true. If another thread has locked the mutex, this function returns false immediately.</p>
<p>If the lock was obtained, the mutex must be unlocked with <a href="qmutex.html#unlock">unlock</a>() before another thread can successfully lock it.</p>
<p>Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a <a href="qmutex.html#RecursionMode-enum">recursive mutex</a>. If this mutex is a <a href="qmutex.html#RecursionMode-enum">non-recursive mutex</a>, this function will <i>always</i> return false when attempting to lock the mutex recursively.</p>
<p>See also <a href="qmutex.html#lock">lock</a>() and <a href="qmutex.html#unlock">unlock</a>().</p>
<h3 class="fn"><a name="tryLock-2"></a>bool QMutex::tryLock ( int <i>timeout</i> )</h3>
<p>This is an overloaded function.</p>
<p>Attempts to lock the mutex. This function returns true if the lock was obtained; otherwise it returns false. If another thread has locked the mutex, this function will wait for at most <i>timeout</i> milliseconds for the mutex to become available.</p>
<p>Note: Passing a negative number as the <i>timeout</i> is equivalent to calling <a href="qmutex.html#lock">lock</a>(), i.e&#x2e; this function will wait forever until mutex can be locked if <i>timeout</i> is negative.</p>
<p>If the lock was obtained, the mutex must be unlocked with <a href="qmutex.html#unlock">unlock</a>() before another thread can successfully lock it.</p>
<p>Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a <a href="qmutex.html#RecursionMode-enum">recursive mutex</a>. If this mutex is a <a href="qmutex.html#RecursionMode-enum">non-recursive mutex</a>, this function will <i>always</i> return false when attempting to lock the mutex recursively.</p>
<p>See also <a href="qmutex.html#lock">lock</a>() and <a href="qmutex.html#unlock">unlock</a>().</p>
<a name="//apple_ref/cpp/instm/QMutex/unlock"></a>
<h3 class="fn"><a name="unlock"></a>void QMutex::unlock ()</h3>
<p>Unlocks the mutex. Attempting to unlock a mutex in a different thread to the one that locked it results in an error. Unlocking a mutex that is not locked results in undefined behavior.</p>
<p>See also <a href="qmutex.html#lock">lock</a>().</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>