350 lines
21 KiB
HTML
350 lines
21 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<!-- qmutex.cpp -->
|
|
<title>Qt 4.8: QMutex Class Reference</title>
|
|
<link rel="stylesheet" type="text/css" href="style/style.css" />
|
|
<script src="scripts/jquery.js" type="text/javascript"></script>
|
|
<script src="scripts/functions.js" type="text/javascript"></script>
|
|
<link rel="stylesheet" type="text/css" href="style/superfish.css" />
|
|
<link rel="stylesheet" type="text/css" href="style/narrow.css" />
|
|
<!--[if IE]>
|
|
<meta name="MSSmartTagsPreventParsing" content="true">
|
|
<meta http-equiv="imagetoolbar" content="no">
|
|
<![endif]-->
|
|
<!--[if lt IE 7]>
|
|
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
|
|
<![endif]-->
|
|
<!--[if IE 7]>
|
|
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
|
|
<![endif]-->
|
|
<!--[if IE 8]>
|
|
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
|
|
<![endif]-->
|
|
|
|
<script src="scripts/superfish.js" type="text/javascript"></script>
|
|
<script src="scripts/narrow.js" type="text/javascript"></script>
|
|
|
|
</head>
|
|
<body class="" onload="CheckEmptyAndLoadList();">
|
|
<div class="header" id="qtdocheader">
|
|
<div class="content">
|
|
<div id="nav-logo">
|
|
<a href="index.html">Home</a></div>
|
|
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
|
|
<div id="narrowsearch"></div>
|
|
<div id="nav-topright">
|
|
<ul>
|
|
<li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
|
|
<li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
|
|
<li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
|
|
DOC</a></li>
|
|
<li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
|
|
</ul>
|
|
</div>
|
|
<div id="shortCut">
|
|
<ul>
|
|
<li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
|
|
<li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li>
|
|
</ul>
|
|
</div>
|
|
<ul class="sf-menu" id="narrowmenu">
|
|
<li><a href="#">API Lookup</a>
|
|
<ul>
|
|
<li><a href="classes.html">Class index</a></li>
|
|
<li><a href="functions.html">Function index</a></li>
|
|
<li><a href="modules.html">Modules</a></li>
|
|
<li><a href="namespaces.html">Namespaces</a></li>
|
|
<li><a href="qtglobal.html">Global Declarations</a></li>
|
|
<li><a href="qdeclarativeelements.html">QML elements</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#">Qt Topics</a>
|
|
<ul>
|
|
<li><a href="qt-basic-concepts.html">Programming with Qt</a></li>
|
|
<li><a href="qtquick.html">Device UIs & Qt Quick</a></li>
|
|
<li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>
|
|
<li><a href="supported-platforms.html">Supported Platforms</a></li>
|
|
<li><a href="technology-apis.html">Qt and Key Technologies</a></li>
|
|
<li><a href="best-practices.html">How-To's and Best Practices</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#">Examples</a>
|
|
<ul>
|
|
<li><a href="all-examples.html">Examples</a></li>
|
|
<li><a href="tutorials.html">Tutorials</a></li>
|
|
<li><a href="demos.html">Demos</a></li>
|
|
<li><a href="qdeclarativeexamples.html">QML Examples</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="wrapper">
|
|
<div class="hd">
|
|
<span></span>
|
|
</div>
|
|
<div class="bd group">
|
|
<div class="sidebar">
|
|
<div class="searchlabel">
|
|
Search index:</div>
|
|
<div class="search" id="sidebarsearch">
|
|
<form id="qtdocsearch" action="" onsubmit="return false;">
|
|
<fieldset>
|
|
<input type="text" name="searchstring" id="pageType" value="" />
|
|
<div id="resultdialog">
|
|
<a href="#" id="resultclose">Close</a>
|
|
<p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p>
|
|
<p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span> results:</p>
|
|
<ul id="resultlist" class="all">
|
|
</ul>
|
|
</div>
|
|
</fieldset>
|
|
</form>
|
|
</div>
|
|
<div class="box first bottombar" id="lookup">
|
|
<h2 title="API Lookup"><span></span>
|
|
API Lookup</h2>
|
|
<div id="list001" class="list">
|
|
<ul id="ul001" >
|
|
<li class="defaultLink"><a href="classes.html">Class index</a></li>
|
|
<li class="defaultLink"><a href="functions.html">Function index</a></li>
|
|
<li class="defaultLink"><a href="modules.html">Modules</a></li>
|
|
<li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
|
|
<li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
|
|
<li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="box bottombar" id="topics">
|
|
<h2 title="Qt Topics"><span></span>
|
|
Qt Topics</h2>
|
|
<div id="list002" class="list">
|
|
<ul id="ul002" >
|
|
<li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li>
|
|
<li class="defaultLink"><a href="qtquick.html">Device UIs & Qt Quick</a></li>
|
|
<li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li>
|
|
<li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>
|
|
<li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li>
|
|
<li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="box" id="examples">
|
|
<h2 title="Examples"><span></span>
|
|
Examples</h2>
|
|
<div id="list003" class="list">
|
|
<ul id="ul003">
|
|
<li class="defaultLink"><a href="all-examples.html">Examples</a></li>
|
|
<li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
|
|
<li class="defaultLink"><a href="demos.html">Demos</a></li>
|
|
<li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="wrap">
|
|
<div class="toolbar">
|
|
<div class="breadcrumb toolblock">
|
|
<ul>
|
|
<li class="first"><a href="index.html">Home</a></li>
|
|
<!-- Breadcrumbs go here -->
|
|
<li><a href="modules.html">Modules</a></li>
|
|
<li><a href="qtcore.html">QtCore</a></li>
|
|
<li>QMutex</li>
|
|
</ul>
|
|
</div>
|
|
<div class="toolbuttons toolblock">
|
|
<ul>
|
|
<li id="smallA" class="t_button">A</li>
|
|
<li id="medA" class="t_button active">A</li>
|
|
<li id="bigA" class="t_button">A</li>
|
|
<li id="print" class="t_button"><a href="javascript:this.print();">
|
|
<span>Print</span></a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="content mainContent">
|
|
<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>
|
|
<h1 class="title">QMutex Class Reference</h1>
|
|
<!-- $$$QMutex-brief -->
|
|
<p>The QMutex class provides access serialization between threads. <a href="#details">More...</a></p>
|
|
<!-- @@@QMutex -->
|
|
<pre class="cpp"> <span class="preprocessor">#include <QMutex></span></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>
|
|
<a name="public-types"></a>
|
|
<h2>Public Types</h2>
|
|
<table class="alignedsummary">
|
|
<tr><td class="memItemLeft rightAlign topAlign"> enum </td><td class="memItemRight bottomAlign"><b><a href="qmutex.html#RecursionMode-enum">RecursionMode</a></b> { Recursive, NonRecursive }</td></tr>
|
|
</table>
|
|
<a name="public-functions"></a>
|
|
<h2>Public Functions</h2>
|
|
<table class="alignedsummary">
|
|
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qmutex.html#QMutex">QMutex</a></b> ( RecursionMode <i>mode</i> = NonRecursive )</td></tr>
|
|
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qmutex.html#dtor.QMutex">~QMutex</a></b> ()</td></tr>
|
|
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qmutex.html#lock">lock</a></b> ()</td></tr>
|
|
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qmutex.html#tryLock">tryLock</a></b> ()</td></tr>
|
|
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qmutex.html#tryLock-2">tryLock</a></b> ( int <i>timeout</i> )</td></tr>
|
|
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qmutex.html#unlock">unlock</a></b> ()</td></tr>
|
|
</table>
|
|
<a name="details"></a>
|
|
<!-- $$$QMutex-description -->
|
|
<div class="descr">
|
|
<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 class="cpp"> <span class="type">int</span> number <span class="operator">=</span> <span class="number">6</span>;
|
|
|
|
<span class="type">void</span> method1()
|
|
{
|
|
number <span class="operator">*</span><span class="operator">=</span> <span class="number">5</span>;
|
|
number <span class="operator">/</span><span class="operator">=</span> <span class="number">4</span>;
|
|
}
|
|
|
|
<span class="type">void</span> method2()
|
|
{
|
|
number <span class="operator">*</span><span class="operator">=</span> <span class="number">3</span>;
|
|
number <span class="operator">/</span><span class="operator">=</span> <span class="number">2</span>;
|
|
}</pre>
|
|
<p>If these two methods are called in succession, the following happens:</p>
|
|
<pre class="cpp"> <span class="comment">// method1()</span>
|
|
number <span class="operator">*</span><span class="operator">=</span> <span class="number">5</span>; <span class="comment">// number is now 30</span>
|
|
number <span class="operator">/</span><span class="operator">=</span> <span class="number">4</span>; <span class="comment">// number is now 7</span>
|
|
|
|
<span class="comment">// method2()</span>
|
|
number <span class="operator">*</span><span class="operator">=</span> <span class="number">3</span>; <span class="comment">// number is now 21</span>
|
|
number <span class="operator">/</span><span class="operator">=</span> <span class="number">2</span>; <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 class="cpp"> <span class="comment">// Thread 1 calls method1()</span>
|
|
number <span class="operator">*</span><span class="operator">=</span> <span class="number">5</span>; <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 <span class="operator">*</span><span class="operator">=</span> <span class="number">3</span>; <span class="comment">// number is now 90</span>
|
|
number <span class="operator">/</span><span class="operator">=</span> <span class="number">2</span>; <span class="comment">// number is now 45</span>
|
|
|
|
<span class="comment">// Thread 1 finishes executing.</span>
|
|
number <span class="operator">/</span><span class="operator">=</span> <span class="number">4</span>; <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 class="cpp"> <span class="type">QMutex</span> mutex;
|
|
<span class="type">int</span> number <span class="operator">=</span> <span class="number">6</span>;
|
|
|
|
<span class="type">void</span> method1()
|
|
{
|
|
mutex<span class="operator">.</span>lock();
|
|
number <span class="operator">*</span><span class="operator">=</span> <span class="number">5</span>;
|
|
number <span class="operator">/</span><span class="operator">=</span> <span class="number">4</span>;
|
|
mutex<span class="operator">.</span>unlock();
|
|
}
|
|
|
|
<span class="type">void</span> method2()
|
|
{
|
|
mutex<span class="operator">.</span>lock();
|
|
number <span class="operator">*</span><span class="operator">=</span> <span class="number">3</span>;
|
|
number <span class="operator">/</span><span class="operator">=</span> <span class="number">2</span>;
|
|
mutex<span class="operator">.</span>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>
|
|
</div>
|
|
<p><b>See also </b><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>
|
|
<!-- @@@QMutex -->
|
|
<div class="types">
|
|
<h2>Member Type Documentation</h2>
|
|
<!-- $$$RecursionMode$$$NonRecursive$$$Recursive -->
|
|
<h3 class="fn"><a name="RecursionMode-enum"></a>enum QMutex::<span class="name">RecursionMode</span></h3>
|
|
<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"><tt>QMutex::Recursive</tt></td><td class="topAlign"><tt>1</tt></td><td class="topAlign">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 class="topAlign"><tt>QMutex::NonRecursive</tt></td><td class="topAlign"><tt>0</tt></td><td class="topAlign">In this mode, a thread may only lock a mutex once.</td></tr>
|
|
</table>
|
|
<p><b>See also </b><a href="qmutex.html#QMutex">QMutex</a>().</p>
|
|
<!-- @@@RecursionMode -->
|
|
</div>
|
|
<div class="func">
|
|
<h2>Member Function Documentation</h2>
|
|
<!-- $$$QMutex[overload1]$$$QMutexRecursionMode -->
|
|
<h3 class="fn"><a name="QMutex"></a>QMutex::<span class="name">QMutex</span> ( <span class="type"><a href="qmutex.html#RecursionMode-enum">RecursionMode</a></span> <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><b>See also </b><a href="qmutex.html#lock">lock</a>() and <a href="qmutex.html#unlock">unlock</a>().</p>
|
|
<!-- @@@QMutex -->
|
|
<!-- $$$~QMutex[overload1]$$$~QMutex -->
|
|
<h3 class="fn"><a name="dtor.QMutex"></a>QMutex::<span class="name">~QMutex</span> ()</h3>
|
|
<p>Destroys the mutex.</p>
|
|
<p><b>Warning:</b> Destroying a locked mutex may result in undefined behavior.</p>
|
|
<!-- @@@~QMutex -->
|
|
<!-- $$$lock[overload1]$$$lock -->
|
|
<h3 class="fn"><a name="lock"></a><span class="type">void</span> QMutex::<span class="name">lock</span> ()</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><b>See also </b><a href="qmutex.html#unlock">unlock</a>().</p>
|
|
<!-- @@@lock -->
|
|
<!-- $$$tryLock[overload1]$$$tryLock -->
|
|
<h3 class="fn"><a name="tryLock"></a><span class="type">bool</span> QMutex::<span class="name">tryLock</span> ()</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><b>See also </b><a href="qmutex.html#lock">lock</a>() and <a href="qmutex.html#unlock">unlock</a>().</p>
|
|
<!-- @@@tryLock -->
|
|
<!-- $$$tryLock$$$tryLockint -->
|
|
<h3 class="fn"><a name="tryLock-2"></a><span class="type">bool</span> QMutex::<span class="name">tryLock</span> ( <span class="type">int</span> <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. 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><b>See also </b><a href="qmutex.html#lock">lock</a>() and <a href="qmutex.html#unlock">unlock</a>().</p>
|
|
<!-- @@@tryLock -->
|
|
<!-- $$$unlock[overload1]$$$unlock -->
|
|
<h3 class="fn"><a name="unlock"></a><span class="type">void</span> QMutex::<span class="name">unlock</span> ()</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><b>See also </b><a href="qmutex.html#lock">lock</a>().</p>
|
|
<!-- @@@unlock -->
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="ft">
|
|
<span></span>
|
|
</div>
|
|
</div>
|
|
<div class="footer">
|
|
<p>
|
|
<acronym title="Copyright">©</acronym> 2015 The Qt Company Ltd.
|
|
Documentation contributions included herein are the copyrights of
|
|
their respective owners.</p>
|
|
<br />
|
|
<p>
|
|
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.</p>
|
|
<p>
|
|
Documentation sources may be obtained from <a href="http://www.qt-project.org">
|
|
www.qt-project.org</a>.</p>
|
|
<br />
|
|
<p>
|
|
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. <a title="Privacy Policy"
|
|
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
|
|
</div>
|
|
|
|
<script src="scripts/functions.js" type="text/javascript"></script>
|
|
</body>
|
|
</html>
|