QMutex 類

QMutex 類提供在綫程之間串行化訪問。 更多...

頭: #include <QMutex>
qmake: QT += core
繼承者:

QRecursiveMutex

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

公共類型

enum RecursionMode { Recursive, NonRecursive }

公共函數

QMutex (QMutex::RecursionMode mode )
QMutex ()
~QMutex ()
bool isRecursive () const
void lock ()
bool tryLock (int timeout = 0)
bool try_lock ()
bool try_lock_for (std::chrono::duration<Rep, Period> duration )
bool try_lock_until (std::chrono::time_point<Clock, Duration> timePoint )
void unlock ()

詳細描述

QMutex 的目的是保護對象、數據結構或代碼區間,以便每次僅有一綫程可以訪問它 (這類似於 Java synchronized 關鍵詞)。通常使用互斥最好采用 QMutexLocker 因為這使之能輕鬆確保鎖定和解鎖的一緻履行。

例如,假定有方法每 2 行嚮用戶打印消息:

int number = 6;
void method1()
{
    number *= 5;
    number /= 4;
}
void method2()
{
    number *= 3;
    number /= 2;
}
					

若連續調用這 2 方法,會發生以下:

// method1()
number *= 5;        // number is now 30
number /= 4;        // number is now 7
// method2()
number *= 3;        // number is now 21
number /= 2;        // number is now 10
					

若從 2 綫程同時調用這 2 方法,就會産生以下序列:

// Thread 1 calls method1()
number *= 5;        // number is now 30
// Thread 2 calls method2().
//
// Most likely Thread 1 has been put to sleep by the operating
// system to allow Thread 2 to run.
number *= 3;        // number is now 90
number /= 2;        // number is now 45
// Thread 1 finishes executing.
number /= 4;        // number is now 11, instead of 10
					

若添加互斥,應該獲得希望結果:

QMutex mutex;
int number = 6;
void method1()
{
    mutex.lock();
    number *= 5;
    number /= 4;
    mutex.unlock();
}
void method2()
{
    mutex.lock();
    number *= 3;
    number /= 2;
    mutex.unlock();
}
					

那麼僅一綫程可以修改 編號 在任何給定時間且結果是正確的。當然,這是通俗範例,但適用於事情需要按特定序列發生的任何其它情況。

當調用 lock () 在綫程中,其它綫程試著調用 lock () 在同一位置將阻塞,直到綫程獲得鎖調用 unlock ()。非阻塞替代 lock () 是 tryLock ().

QMutex 經優化在非競爭情況下很快。非遞歸 QMutex 不會分配內存,若互斥不被爭用。它的構造和銷毀幾乎沒有開銷,這意味著將很多互斥作為其它類的一部分很好。

另請參閱 QRecursiveMutex , QMutexLocker , QReadWriteLock , QSemaphore ,和 QWaitCondition .

成員類型文檔編製

enum QMutex:: RecursionMode

常量 描述
QMutex::Recursive 1 In this mode, a thread can lock the same mutex multiple times and the mutex won't be unlocked until a corresponding number of unlock () calls have been made. You should use QRecursiveMutex for this use-case.
QMutex::NonRecursive 0 In this mode, a thread may only lock a mutex once.

另請參閱 QMutex () 和 QRecursiveMutex .

成員函數文檔編製

QMutex:: QMutex ( QMutex::RecursionMode mode )

構造新的互斥。互斥是在解鎖狀態下創建的。

mode is QMutex::Recursive , a thread can lock the same mutex multiple times and the mutex won't be unlocked until a corresponding number of unlock () calls have been made. Otherwise a thread may only lock a mutex once. The default is QMutex::NonRecursive .

Recursive mutexes are slower and take more memory than non-recursive ones.

另請參閱 lock () 和 unlock ().

QMutex:: QMutex ()

構造新的互斥。互斥是在解鎖狀態下創建的。

QMutex:: ~QMutex ()

銷毀互斥。

警告: 銷毀鎖定互斥可能導緻未定義行為。

bool QMutex:: isRecursive () const

返迴 true 若互斥是遞歸的。

該函數在 Qt 5.7 引入。

void QMutex:: lock ()

Locks the mutex. If another thread has locked the mutex then this call will block until that thread has unlocked it.

Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a 遞歸互斥 。若此互斥是 非遞歸互斥 ,此函數將 dead-lock when the mutex is locked recursively.

另請參閱 unlock ().

bool QMutex:: tryLock ( int timeout = 0)

試圖鎖定互斥。此函數返迴 true 若獲得鎖;否則返迴 false . If another thread has locked the mutex, this function will wait for at most timeout milliseconds for the mutex to become available.

注意:傳遞負數作為 timeout 相當於調用 lock (), i.e. this function will wait forever until mutex can be locked if timeout 為負。

If the lock was obtained, the mutex must be unlocked with unlock () 在另一綫程可以成功鎖定它之前。

Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a 遞歸互斥 。若此互斥是 非遞歸互斥 ,此函數將 always return false when attempting to lock the mutex recursively.

另請參閱 lock () 和 unlock ().

bool QMutex:: try_lock ()

試圖鎖定互斥。此函數返迴 true 若獲得鎖;否則返迴 false .

提供此函數是為兼容標準庫概念 Lockable 。它相當於 tryLock ().

函數返迴 true 若獲得鎖;否則返迴 false

該函數在 Qt 5.8 引入。

template <typename Rep, typename Period> bool QMutex:: try_lock_for ( std::chrono::duration < Rep , Period > duration )

試圖鎖定互斥。此函數返迴 true 若獲得鎖;否則返迴 false 。若另一綫程有鎖定互斥,此函數將等待至少 duration 為使互斥變得可用。

注意:傳遞負值持續時間作為 duration 相當於調用 try_lock ()。此行為不同於 tryLock ().

If the lock was obtained, the mutex must be unlocked with unlock () 在另一綫程可以成功鎖定它之前。

Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a 遞歸互斥 。若此互斥是 非遞歸互斥 ,此函數將 always return false when attempting to lock the mutex recursively.

該函數在 Qt 5.8 引入。

另請參閱 lock () 和 unlock ().

template <typename Clock, typename Duration> bool QMutex:: try_lock_until ( std::chrono::time_point < Clock , Duration > timePoint )

試圖鎖定互斥。此函數返迴 true 若獲得鎖;否則返迴 false . If another thread has locked the mutex, this function will wait at least until timePoint 為使互斥變得可用。

注意:傳遞 timePoint which has already passed is equivalent to calling try_lock ()。此行為不同於 tryLock ().

If the lock was obtained, the mutex must be unlocked with unlock () 在另一綫程可以成功鎖定它之前。

Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a 遞歸互斥 。若此互斥是 非遞歸互斥 ,此函數將 always return false when attempting to lock the mutex recursively.

該函數在 Qt 5.8 引入。

另請參閱 lock () 和 unlock ().

void QMutex:: unlock ()

Unlocks the mutex. Attempting to unlock a mutex in a different thread to the one that locked it results in an error. Unlocking a mutex that is not locked results in undefined behavior.

另請參閱 lock ().