QMutexLocker 类

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

头: #include <QMutexLocker>
qmake: QT += core

注意: 此类的所有函数 thread-safe .

公共函数

QMutexLocker (QMutex * mutex )
~QMutexLocker ()
QMutex * mutex () const
void relock ()
void unlock ()

详细描述

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

锁定和解锁 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 被销毁。

例如,此复杂函数锁定 QMutex 当进入函数时并在所有退出点解锁互斥:

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;
}
					

此范例函数在开发过程中将变得更复杂,从而增加了发生错误的可能性。

使用 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 是自动变量)。

相同原理也适用于抛出、捕捉异常的代码。在锁定互斥的函数中未被捕获的异常没有办法解锁互斥,在把异常向上传递给调用函数的堆栈之前。

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 ()。例如:

class SignalWaiter
{
private:
    QMutexLocker locker;
public:
    SignalWaiter(QMutex *mutex)
        : locker(mutex)
    {
    }
    void waitForSignal()
    {
        ...
        while (!signalled)
            waitCondition.wait(locker.mutex());
        ...
    }
};
					

另请参阅 QReadLocker , QWriteLocker ,和 QMutex .

成员函数文档编制

QMutexLocker:: QMutexLocker ( QMutex * mutex )

构造 QMutexLocker 和锁 mutex . The mutex will be unlocked when the QMutexLocker 被销毁。若 mutex 为 0, QMutexLocker 什么都不做。

另请参阅 QMutex::lock ().

QMutexLocker:: ~QMutexLocker ()

销毁 QMutexLocker 并解锁在构造函数中被锁定的互斥。

另请参阅 QMutex::unlock ().

QMutex *QMutexLocker:: mutex () const

返回互斥在那里 QMutexLocker 正在运转。

void QMutexLocker:: relock ()

重新锁定被解锁的互斥锁定器。

另请参阅 unlock ().

void QMutexLocker:: unlock ()

解锁此互斥锁定器。可以使用 relock() 以再次锁定它。它不需要被锁定当销毁时。

另请参阅 relock ().