QRecursiveMutex 类

QRecursiveMutex 类提供在线程之间串行化访问。 更多...

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

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

公共函数

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 ().