Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QMutexLocker< Mutex > Class Template Reference

\inmodule QtCore More...

#include <qmutex.h>

+ Collaboration diagram for QMutexLocker< Mutex >:

Public Member Functions

Q_NODISCARD_CTOR QMutexLocker (Mutex *) noexcept
 Constructs a QMutexLocker and locks mutex.
 
 ~QMutexLocker () noexcept
 Destroys the QMutexLocker and unlocks the mutex that was locked in the constructor.
 
void unlock () noexcept
 Unlocks this mutex locker.
 
void relock () noexcept
 Relocks an unlocked mutex locker.
 
Mutex * mutex () const noexcept
 Returns the mutex on which the QMutexLocker is operating.
 

Detailed Description

template<typename Mutex>
class QMutexLocker< Mutex >

\inmodule QtCore

The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes.

\threadsafe

Locking and unlocking a QMutex or QRecursiveMutex 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.

QMutexLocker should be created within a function where a QMutex needs to be locked. The mutex is locked when QMutexLocker is created. You can unlock and relock the mutex with unlock() and relock(). If locked, the mutex will be unlocked when the QMutexLocker is destroyed.

For example, this complex function locks a QMutex upon entering the function and unlocks the mutex at all the exit points:

int complexFunction(int flag)
{
int retVal = 0;
switch (flag) {
case 0:
case 1:
retVal = moreComplexFunction(flag);
break;
case 2:
{
int status = anotherFunction();
if (status < 0) {
return -2;
}
retVal = status + flag;
}
break;
default:
if (flag > 10) {
return -1;
}
break;
}
return retVal;
}

This example function will get more complicated as it is developed, which increases the likelihood that errors will occur.

Using QMutexLocker greatly simplifies the code, and makes it more readable:

int complexFunction(int flag)
{
QMutexLocker locker(&mutex);
int retVal = 0;
switch (flag) {
case 0:
case 1:
return moreComplexFunction(flag);
case 2:
{
int status = anotherFunction();
if (status < 0)
return -2;
retVal = status + flag;
}
break;
default:
if (flag > 10)
return -1;
break;
}
return retVal;
}

Now, the mutex will always be unlocked when the QMutexLocker object is destroyed (when the function returns since locker is an auto variable).

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.

QMutexLocker also provides a mutex() 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 QWaitCondition::wait(). For example:

{
private:
QMutexLocker<QMutex> locker;
public:
: locker(mutex)
{
}
{
...
while (!signalled)
waitCondition.wait(locker.mutex());
...
}
};
\inmodule QtCore
Definition qmutex.h:313
\inmodule QtCore
Definition qmutex.h:281
void unlock() noexcept
Unlocks the mutex.
Definition qmutex.h:289
void lock() noexcept
Locks the mutex.
Definition qmutex.h:286
See also
QReadLocker, QWriteLocker, QMutex

Definition at line 312 of file qmutex.h.

Constructor & Destructor Documentation

◆ QMutexLocker()

template<typename Mutex >
template< typename Mutex > QMutexLocker< Mutex >::QMutexLocker ( Mutex * mutex)
inlineexplicitnoexcept

Constructs a QMutexLocker and locks mutex.

The mutex will be unlocked when the QMutexLocker is destroyed. If mutex is \nullptr, QMutexLocker does nothing.

See also
QMutex::lock()
Since
6.4

Move-constructs a QMutexLocker from other. The mutex and the state of other is transferred to the newly constructed instance. After the move, other will no longer be managing any mutex.

See also
QMutex::lock()

Definition at line 316 of file qmutex.h.

◆ ~QMutexLocker()

template<typename Mutex >
template< typename Mutex > QMutexLocker< Mutex >::~QMutexLocker ( )
inlinenoexcept

Destroys the QMutexLocker and unlocks the mutex that was locked in the constructor.

See also
QMutex::unlock()

Definition at line 317 of file qmutex.h.

Member Function Documentation

◆ mutex()

template<typename Mutex >
template< typename Mutex > QMutex * QMutexLocker< Mutex >::mutex ( ) const
inlinenoexcept

Returns the mutex on which the QMutexLocker is operating.

Definition at line 321 of file qmutex.h.

Referenced by QThreadPoolThread::run().

+ Here is the caller graph for this function:

◆ relock()

template<typename Mutex >
template< typename Mutex > void QMutexLocker< Mutex >::relock ( )
inlinenoexcept

Relocks an unlocked mutex locker.

See also
unlock()

Definition at line 320 of file qmutex.h.

Referenced by QObject::~QObject(), AdapterManager::addClient(), QThreadPoolPrivate::clear(), QTzTimeZoneCache::fetchEntry(), queued_activate(), QThreadPoolThread::run(), QLibraryPrivate::updatePluginState(), and QSignalSpy::wait().

+ Here is the caller graph for this function:

◆ unlock()


The documentation for this class was generated from the following files: