<class name="QMutex" doc="/** <p>The <a href="QMutex.html#QMutex(com.trolltech.qt.core.QMutex.RecursionMode)"><tt>QMutex</tt></a> class provides access serialization between threads.</p> <p>The purpose of a <a href="QMutex.html#QMutex(com.trolltech.qt.core.QMutex.RecursionMode)"><tt>QMutex</tt></a> 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 QMutexLocker 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()"><tt>lock</tt></a> in a thread, other threads that try to call <a href="QMutex.html#lock()"><tt>lock</tt></a> in the same place will block until the thread that got the lock calls <a href="QMutex.html#unlock()"><tt>unlock</tt></a>. A non-blocking alternative to <a href="QMutex.html#lock()"><tt>lock</tt></a> is <a href="QMutex.html#tryLock(int)"><tt>tryLock</tt></a>.</p> @see <tt>QMutexLocker</tt> @see <a href="%2E%2E/gui/QReadWriteLock.html"><tt>QReadWriteLock</tt></a> @see <a href="QSemaphore.html"><tt>QSemaphore</tt></a> @see <a href="QWaitCondition.html"><tt>QWaitCondition</tt></a> */"> <method name="public QMutex(com.trolltech.qt.core.QMutex.RecursionMode mode)" doc="/** <p>Constructs a new mutex. The mutex is created in an unlocked state.</p> <p>If <tt>mode</tt> is QMutex::Recursive, 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()"><tt>unlock</tt></a> calls have been made. The default is QMutex::NonRecursive.</p> @see <a href="QMutex.html#lock()"><tt>lock</tt></a> @see <a href="QMutex.html#unlock()"><tt>unlock</tt></a> */"/> <method name="public QMutex()" doc="/** <p>Equivalent to <a href="QMutex.html#QMutex(com.trolltech.qt.core.QMutex.RecursionMode)"><tt>QMutex</tt></a>(NonRecursive). */"/> <method name="public final void lock()" doc="/** <p>Locks the mutex. If another thread has locked the mutex then this call will block until that thread has unlocked it.</p> @see <a href="QMutex.html#unlock()"><tt>unlock</tt></a> */"/> <method name="public final boolean tryLock()" doc="/** <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()"><tt>unlock</tt></a> before another thread can successfully lock it.</p> @see <a href="QMutex.html#lock()"><tt>lock</tt></a> @see <a href="QMutex.html#unlock()"><tt>unlock</tt></a> */"/> <method name="public final boolean tryLock(int timeout)" doc="/** <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 <tt>timeout</tt> milliseconds for the mutex to become available.</p> <p>Note: Passing a negative number as the <tt>timeout</tt> is equivalent to calling <a href="QMutex.html#lock()"><tt>lock</tt></a>, i.e&#x2e; this function will wait forever until mutex can be locked if <tt>timeout</tt> is negative.</p> <p>If the lock was obtained, the mutex must be unlocked with <a href="QMutex.html#unlock()"><tt>unlock</tt></a> before another thread can successfully lock it.</p> @see <a href="QMutex.html#lock()"><tt>lock</tt></a> @see <a href="QMutex.html#unlock()"><tt>unlock</tt></a> */"/> <method name="public final void unlock()" doc="/** <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> @see <a href="QMutex.html#lock()"><tt>lock</tt></a> */"/> <enum name="RecursionMode"> <enum-value name="NonRecursive" doc="/** <p>In this mode, a thread may only lock a mutex once.</p> */"/> <enum-value name="Recursive" doc="/** <p>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()"><tt>unlock</tt></a> calls have been made.</p> */"/> </enum> </class>