CopperSpice API
1.9.2
|
Simplifies locking and unlocking mutexes. More...
Public Methods | |
QMutexLocker (QMutex *mutex) | |
~QMutexLocker () = default | |
void | lock () |
QMutex * | mutex () const |
void | relock () |
void | unlock () |
The QMutexLocker class simplifies locking and unlocking mutexes. Locking and unlocking a QMutex manually in complex functions or in exception handling code is error prone and can be difficult to debug. QMutexLocker can be used in such situations to ensure the state of the mutex is always well defined.
The mutex will be 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.
The QRecursiveMutexLocker class provides the equivalent functionality for code which uses a recursive mutex.
In this example the function locks a QMutex at the beginning and unlocks the mutex at all the exit points.
Using QMutexLocker will simplify the code and improves readable. The code below has been modified to use QMutexLocker. In this new code the mutex will always be unlocked when the QMutexLocker object is destroyed. QMutexLocker is also useful for code which might throw or catch exception.
QMutexLocker provides a mutex() method which returns the mutex originally passed in the constructor. This is useful for code that needs access to the mutex, such as QWaitCondition::wait().
|
inlineexplicit |
Constructs a QMutexLocker and locks mutex. The mutex will be unlocked when the QMutexLocker is destroyed. If mutex is a nullptr QMutexLocker does nothing.
|
default |
Destroys the QMutexLocker and unlocks the mutex that was locked in the constructor.
|
inline |
|
inline |
Returns a pointer to the mutex which was locked in the constructor.
|
inline |
Equivalent to calling lock().