QRecursiveMutex 類

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

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

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

公共函數

QRecursiveMutex ()
~QRecursiveMutex ()
void lock ()
(從 6.6 起) bool tryLock (QDeadlineTimer timeout = {})
bool tryLock (int timeout )
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 ()

詳細描述

QRecursiveMutex 類是互斥的像 QMutex ,采用可兼容 API。它不同於 QMutex 通過接受 lock () 調用來自同一綫程任意次數。 QMutex 在這種情況下,會死鎖。

QRecursiveMutex is much more expensive to construct and operate on, so use a plain QMutex whenever you can. Sometimes, one public function, however, calls another public function, and they both need to lock the same mutex. In this case, you have two options:

  • Factor the code that needs mutex protection into private functions, which assume that the mutex is held when they are called, and lock a plain QMutex in the public functions before you call the private implementation ones.
  • Or use a recursive mutex, so it doesn't matter that the first public function has already locked the mutex when the second one wishes to do so.

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

成員函數文檔編製

[constexpr noexcept] QRecursiveMutex:: QRecursiveMutex ()

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

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

[noexcept] QRecursiveMutex:: ~QRecursiveMutex ()

銷毀互斥。

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

[noexcept] void QRecursiveMutex:: 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.

另請參閱 unlock ().

[noexcept, since 6.6] bool QRecursiveMutex:: tryLock ( QDeadlineTimer timeout = {})

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

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.

該函數在 Qt 6.6 引入。

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

[noexcept] bool QRecursiveMutex:: tryLock ( int timeout )

試圖鎖定互斥。此函數返迴 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.

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

[noexcept] bool QRecursiveMutex:: try_lock ()

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

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

template <typename Rep, typename Period> bool QRecursiveMutex:: 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.

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

template <typename Clock, typename Duration> bool QRecursiveMutex:: 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.

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

[noexcept] void QRecursiveMutex:: 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 ().