<class name="QMutex" doc="/**
&lt;p&gt;The &lt;a href=&quot;QMutex.html#QMutex(com.trolltech.qt.core.QMutex.RecursionMode)&quot;&gt;&lt;tt&gt;QMutex&lt;/tt&gt;&lt;/a&gt; class provides access serialization between threads.&lt;/p&gt;
&lt;p&gt;The purpose of a &lt;a href=&quot;QMutex.html#QMutex(com.trolltech.qt.core.QMutex.RecursionMode)&quot;&gt;&lt;tt&gt;QMutex&lt;/tt&gt;&lt;/a&gt; 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 &lt;tt&gt;synchronized&lt;/tt&gt; keyword). It is usually best to use a mutex with a QMutexLocker since this makes it easy to ensure that locking and unlocking are performed consistently.&lt;/p&gt;
&lt;p&gt;For example, say there is a method that prints a message to the user on two lines:&lt;/p&gt;
&lt;pre&gt;    int number = 6;

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

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

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

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

&lt;span class=&quot;comment&quot;&gt;    //&lt;/span&gt; Thread 1 finishes executing.
    number /= 4;        &lt;span class=&quot;comment&quot;&gt;//&lt;/span&gt; number is now 11, instead of 10&lt;/pre&gt;
&lt;p&gt;If we add a mutex, we should get the result we want:&lt;/p&gt;
&lt;pre&gt;    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();
    }&lt;/pre&gt;
&lt;p&gt;Then only one thread can modify &lt;tt&gt;number&lt;/tt&gt; 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.&lt;/p&gt;
&lt;p&gt;When you call &lt;a href=&quot;QMutex.html#lock()&quot;&gt;&lt;tt&gt;lock&lt;/tt&gt;&lt;/a&gt; in a thread, other threads that try to call &lt;a href=&quot;QMutex.html#lock()&quot;&gt;&lt;tt&gt;lock&lt;/tt&gt;&lt;/a&gt; in the same place will block until the thread that got the lock calls &lt;a href=&quot;QMutex.html#unlock()&quot;&gt;&lt;tt&gt;unlock&lt;/tt&gt;&lt;/a&gt;. A non-blocking alternative to &lt;a href=&quot;QMutex.html#lock()&quot;&gt;&lt;tt&gt;lock&lt;/tt&gt;&lt;/a&gt; is &lt;a href=&quot;QMutex.html#tryLock(int)&quot;&gt;&lt;tt&gt;tryLock&lt;/tt&gt;&lt;/a&gt;.&lt;/p&gt;

@see &lt;tt&gt;QMutexLocker&lt;/tt&gt;
@see &lt;a href=&quot;%2E%2E/gui/QReadWriteLock.html&quot;&gt;&lt;tt&gt;QReadWriteLock&lt;/tt&gt;&lt;/a&gt;
@see &lt;a href=&quot;QSemaphore.html&quot;&gt;&lt;tt&gt;QSemaphore&lt;/tt&gt;&lt;/a&gt;
@see &lt;a href=&quot;QWaitCondition.html&quot;&gt;&lt;tt&gt;QWaitCondition&lt;/tt&gt;&lt;/a&gt; */">
    <method name="public QMutex(com.trolltech.qt.core.QMutex.RecursionMode mode)" doc="/**
&lt;p&gt;Constructs a new mutex. The mutex is created in an unlocked state.&lt;/p&gt;
&lt;p&gt;If &lt;tt&gt;mode&lt;/tt&gt; is QMutex::Recursive, a thread can lock the same mutex multiple times and the mutex won't be unlocked until a corresponding number of &lt;a href=&quot;QMutex.html#unlock()&quot;&gt;&lt;tt&gt;unlock&lt;/tt&gt;&lt;/a&gt; calls have been made. The default is QMutex::NonRecursive.&lt;/p&gt;

@see &lt;a href=&quot;QMutex.html#lock()&quot;&gt;&lt;tt&gt;lock&lt;/tt&gt;&lt;/a&gt;
@see &lt;a href=&quot;QMutex.html#unlock()&quot;&gt;&lt;tt&gt;unlock&lt;/tt&gt;&lt;/a&gt; */"/>
    <method name="public QMutex()" doc="/**
&lt;p&gt;Equivalent to &lt;a href=&quot;QMutex.html#QMutex(com.trolltech.qt.core.QMutex.RecursionMode)&quot;&gt;&lt;tt&gt;QMutex&lt;/tt&gt;&lt;/a&gt;(NonRecursive). */"/>
    <method name="public final void lock()" doc="/**
&lt;p&gt;Locks the mutex. If another thread has locked the mutex then this call will block until that thread has unlocked it.&lt;/p&gt;

@see &lt;a href=&quot;QMutex.html#unlock()&quot;&gt;&lt;tt&gt;unlock&lt;/tt&gt;&lt;/a&gt; */"/>
    <method name="public final boolean tryLock()" doc="/**
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;If the lock was obtained, the mutex must be unlocked with &lt;a href=&quot;QMutex.html#unlock()&quot;&gt;&lt;tt&gt;unlock&lt;/tt&gt;&lt;/a&gt; before another thread can successfully lock it.&lt;/p&gt;

@see &lt;a href=&quot;QMutex.html#lock()&quot;&gt;&lt;tt&gt;lock&lt;/tt&gt;&lt;/a&gt;
@see &lt;a href=&quot;QMutex.html#unlock()&quot;&gt;&lt;tt&gt;unlock&lt;/tt&gt;&lt;/a&gt; */"/>
    <method name="public final boolean tryLock(int timeout)" doc="/**
&lt;p&gt;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 &lt;tt&gt;timeout&lt;/tt&gt; milliseconds for the mutex to become available.&lt;/p&gt;
&lt;p&gt;Note: Passing a negative number as the &lt;tt&gt;timeout&lt;/tt&gt; is equivalent to calling &lt;a href=&quot;QMutex.html#lock()&quot;&gt;&lt;tt&gt;lock&lt;/tt&gt;&lt;/a&gt;, i.e&amp;#x2e; this function will wait forever until mutex can be locked if &lt;tt&gt;timeout&lt;/tt&gt; is negative.&lt;/p&gt;
&lt;p&gt;If the lock was obtained, the mutex must be unlocked with &lt;a href=&quot;QMutex.html#unlock()&quot;&gt;&lt;tt&gt;unlock&lt;/tt&gt;&lt;/a&gt; before another thread can successfully lock it.&lt;/p&gt;

@see &lt;a href=&quot;QMutex.html#lock()&quot;&gt;&lt;tt&gt;lock&lt;/tt&gt;&lt;/a&gt;
@see &lt;a href=&quot;QMutex.html#unlock()&quot;&gt;&lt;tt&gt;unlock&lt;/tt&gt;&lt;/a&gt; */"/>
    <method name="public final void unlock()" doc="/**
&lt;p&gt;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.&lt;/p&gt;

@see &lt;a href=&quot;QMutex.html#lock()&quot;&gt;&lt;tt&gt;lock&lt;/tt&gt;&lt;/a&gt; */"/>
    <enum name="RecursionMode">
        <enum-value name="NonRecursive" doc="/**
&lt;p&gt;In this mode, a thread may only lock a mutex once.&lt;/p&gt;
 */"/>
        <enum-value name="Recursive" doc="/**
&lt;p&gt;In this mode, a thread can lock the same mutex multiple times and the mutex won't be unlocked until a corresponding number of &lt;a href=&quot;QMutex.html#unlock()&quot;&gt;&lt;tt&gt;unlock&lt;/tt&gt;&lt;/a&gt; calls have been made.&lt;/p&gt;
 */"/>
</enum>
</class>
