Struct qt_core::QMutexLocker

source ·
#[repr(C)]
pub struct QMutexLocker { /* private fields */ }
Expand description

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

C++ class: QMutexLocker.

C++ documentation:

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

Locking and unlocking a QMutex 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 < 0) { mutex.unlock(); return -2; } retVal = status + flag; } break; default: if (flag > 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(&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:

class SignalWaiter { private: QMutexLocker locker;

public: SignalWaiter(QMutex *mutex) : locker(mutex) { }

void waitForSignal() { ... while (!signalled) waitCondition.wait(locker.mutex()); ... } };

Implementations§

source§

impl QMutexLocker

source

pub unsafe fn mutex(&self) -> Ptr<QMutex>

Returns the mutex on which the QMutexLocker is operating.

Calls C++ function: QMutex* QMutexLocker::mutex() const.

C++ documentation:

Returns the mutex on which the QMutexLocker is operating.

source

pub unsafe fn from_q_basic_mutex( m: impl CastInto<Ptr<QBasicMutex>> ) -> CppBox<QMutexLocker>

Constructs a QMutexLocker and locks mutex. The mutex will be unlocked when the QMutexLocker is destroyed. If mutex is zero, QMutexLocker does nothing.

Calls C++ function: [constructor] void QMutexLocker::QMutexLocker(QBasicMutex* m).

Warning: no exact match found in C++ documentation. Below is the C++ documentation for QMutexLocker::QMutexLocker(QMutex *mutex):

Constructs a QMutexLocker and locks mutex. The mutex will be unlocked when the QMutexLocker is destroyed. If mutex is zero, QMutexLocker does nothing.

See also QMutex::lock().

source

pub unsafe fn from_q_recursive_mutex( m: impl CastInto<Ptr<QRecursiveMutex>> ) -> CppBox<QMutexLocker>

Available on cpp_lib_version="5.14.0" only.

Constructs a QMutexLocker and locks mutex. The mutex will be unlocked (unlock() called) when the QMutexLocker is destroyed. If mutex is nullptr, QMutexLocker does nothing.

Calls C++ function: [constructor] void QMutexLocker::QMutexLocker(QRecursiveMutex* m).

C++ documentation:

Constructs a QMutexLocker and locks mutex. The mutex will be unlocked (unlock() called) when the QMutexLocker is destroyed. If mutex is nullptr, QMutexLocker does nothing.

This function was introduced in Qt 5.14.

See also QMutex::lock().

source

pub unsafe fn relock(&self)

Relocks an unlocked mutex locker.

Calls C++ function: void QMutexLocker::relock().

C++ documentation:

Relocks an unlocked mutex locker.

See also unlock().

source

pub unsafe fn unlock(&self)

Unlocks this mutex locker. You can use relock() to lock it again. It does not need to be locked when destroyed.

Calls C++ function: void QMutexLocker::unlock().

C++ documentation:

Unlocks this mutex locker. You can use relock() to lock it again. It does not need to be locked when destroyed.

See also relock().

Trait Implementations§

source§

impl CppDeletable for QMutexLocker

source§

unsafe fn delete(&self)

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

Calls C++ function: [destructor] void QMutexLocker::~QMutexLocker().

C++ documentation:

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

See also QMutex::unlock().

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T, U> CastInto<U> for T
where U: CastFrom<T>,

source§

unsafe fn cast_into(self) -> U

Performs the conversion. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> StaticUpcast<T> for T

source§

unsafe fn static_upcast(ptr: Ptr<T>) -> Ptr<T>

Convert type of a const pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.