QMutexLocker Class▲
-
Header: QMutexLocker
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Group: QMutexLocker is part of thread
Detailed Description▲
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)
{
mutex.lock();
int
retVal =
0
;
switch
(flag) {
case
0
:
case
1
:
retVal =
moreComplexFunction(flag);
break
;
case
2
:
{
int
status =
anotherFunction();
if
(status &
lt; 0
) {
mutex.unlock();
return
-
2
;
}
retVal =
status +
flag;
}
break
;
default
:
if
(flag &
gt; 10
) {
mutex.unlock();
return
-
1
;
}
break
;
}
mutex.unlock();
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(&
amp;mutex);
int
retVal =
0
;
switch
(flag) {
case
0
:
case
1
:
return
moreComplexFunction(flag);
case
2
:
{
int
status =
anotherFunction();
if
(status &
lt; 0
)
return
-
2
;
retVal =
status +
flag;
}
break
;
default
:
if
(flag &
gt; 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:
class
SignalWaiter
{
private
:
QMutexLocker&
lt;QMutex&
gt; locker;
public
:
SignalWaiter(QMutex *
mutex)
:
locker(mutex)
{
}
void
waitForSignal()
{
...
while
(!
signalled)
waitCondition.wait(locker.mutex());
...
}
}
;
See Also▲
See also QReadLocker, QWriteLocker, QMutex
Member Function Documentation▲
[explicit] QMutexLocker::QMutexLocker(Mutex *mutex)▲
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▲
See also QMutex::lock()
[since 6.4] QMutexLocker::QMutexLocker(QMutexLocker<Mutex> &&other)▲
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.
This function was introduced in Qt 6.4.
See Also▲
See also QMutex::lock()
QMutexLocker::~QMutexLocker()▲
Destroys the QMutexLocker and unlocks the mutex that was locked in the constructor.
See Also▲
See also QMutex::unlock()
[since 6.4] bool QMutexLocker::isLocked() const▲
Returns true if this QMutexLocker is currently locking its associated mutex, or false otherwise.
This function was introduced in Qt 6.4.
Mutex *QMutexLocker::mutex() const▲
Returns the mutex on which the QMutexLocker is operating.
void QMutexLocker::relock()▲
[since 6.4] void QMutexLocker::swap(QMutexLocker<Mutex> &other)▲
Swaps the mutex and the state of this QMutexLocker with other. This operation is very fast and never fails.
This function was introduced in Qt 6.4.
See Also▲
See also QMutex::lock()
void QMutexLocker::unlock()▲
Unlocks this mutex locker. You can use relock() to lock it again. It does not need to be locked when destroyed.
See Also▲
See also relock()
[since 6.4] QMutexLocker<Mutex> &QMutexLocker::operator=(QMutexLocker<Mutex> &&other)▲
Move-assigns other onto this QMutexLocker. If this QMutexLocker was holding a locked mutex before the assignment, the mutex will be unlocked. The mutex and the state of other is then transferred to this QMutexLocker. After the move, other will no longer be managing any mutex.
This function was introduced in Qt 6.4.
See Also▲
See also QMutex::lock()