QMutexLocker 類

template <typename Mutex> class QMutexLocker

QMutexLocker 類是簡化鎖定和解鎖互斥的方便類。 更多...

頭: #include <QMutexLocker>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意: 此類的所有函數 綫程安全 .

公共函數

QMutexLocker (Mutex * mutex )
(從 6.4 起) QMutexLocker (QMutexLocker<Mutex> && other )
~QMutexLocker ()
(從 6.4 起) bool isLocked () const
Mutex * mutex () const
void relock ()
(從 6.4 起) void swap (QMutexLocker<Mutex> & other )
void unlock ()
(從 6.4 起) QMutexLocker<Mutex> & operator= (QMutexLocker<Mutex> && other )

詳細描述

鎖定和解鎖 QMutex or QRecursiveMutex 在復雜函數、語句 (或異常處理) 中,代碼容易齣錯且難以調試。QMutexLocker 可以用於這種狀況以確保互斥狀態始終定義良好。

應該在函數內創建 QMutexLocker,那裏 QMutex 需要被鎖定。互斥被鎖定當 QMutexLocker 被創建時。可以解鎖和重新鎖定互斥采用 unlock() and relock() 。若被鎖定,互斥會被解鎖當 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 能大大簡化代碼,並使之更可讀:

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

現在,互斥將始終被解鎖當 QMutexLocker 對象被銷毀時 (當函數返迴時,因為 locker 是自動變量)。

相同原理也適用於拋齣、捕捉異常的代碼。在鎖定互斥的函數中未被捕獲的異常沒有辦法解鎖互斥,在把異常嚮上傳遞給調用函數的堆棧之前。

QMutexLocker 還提供 mutex() 成員函數以返迴互斥在那裏 QMutexLocker 正在運轉。這對需要訪問互斥的代碼很有用,譬如 QWaitCondition::wait ()。例如:

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

另請參閱 QReadLocker , QWriteLocker ,和 QMutex .

成員函數文檔編製

[explicit noexcept(...)] QMutexLocker:: QMutexLocker ( Mutex * mutex )

構造 QMutexLocker 和鎖 mutex 。互斥會被解鎖當 QMutexLocker 被銷毀時。若 mutex is nullptr ,QMutexLocker 什麼都不做。

注意: This function is noexcept when LockIsNoexcept is true .

另請參閱 QMutex::lock ().

[noexcept, 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.

該函數在 Qt 6.4 引入。

另請參閱 QMutex::lock ().

QMutexLocker:: ~QMutexLocker ()

銷毀 QMutexLocker 並解鎖在構造函數中被鎖定的互斥。

另請參閱 QMutex::unlock ().

[noexcept, since 6.4] bool QMutexLocker:: isLocked () const

返迴 true,若此 QMutexLocker is currently locking its associated mutex, or false otherwise.

該函數在 Qt 6.4 引入。

Mutex *QMutexLocker:: mutex () const

返迴互斥在那裏 QMutexLocker 正在運轉。

[noexcept(...)] void QMutexLocker:: relock ()

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

注意: This function is noexcept when LockIsNoexcept is true .

另請參閱 unlock ().

[noexcept, since 6.4] void QMutexLocker:: swap ( QMutexLocker < Mutex > & other )

Swaps the mutex and the state of this QMutexLocker with other 。此操作很快且從不失敗。

該函數在 Qt 6.4 引入。

另請參閱 QMutex::lock ().

[noexcept] void QMutexLocker:: unlock ()

解鎖此互斥鎖定器。可以使用 relock() 以再次鎖定它。它不需要被鎖定當銷毀時。

另請參閱 relock ().

[noexcept, since 6.4] QMutexLocker < Mutex > &QMutexLocker:: operator= ( QMutexLocker < Mutex > && other )

移動賦值 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.

該函數在 Qt 6.4 引入。

另請參閱 QMutex::lock ().