QRecursiveMutex 类

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

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

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

公共函数

  QRecursiveMutex ()
  ~QRecursiveMutex ()
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 ()

详细描述

QRecursiveMutex 类是互斥,像 QMutex , with which it is API-compatible. It differs from QMutex by accepting lock () calls from the same thread any number of times. QMutex would deadlock in this situation.

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 .

成员函数文档编制

QRecursiveMutex:: QRecursiveMutex ()

Constructs a new recursive mutex. The mutex is created in an unlocked state.

另请参阅 lock () 和 unlock ().

QRecursiveMutex:: ~QRecursiveMutex ()

销毁互斥。

警告: 销毁锁定互斥可能导致未定义行为。

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

bool QRecursiveMutex:: 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.

另请参阅 lock () 和 unlock ().

[since 5.8] bool QRecursiveMutex:: try_lock ()

试图锁定互斥。此函数返回 true 若获得锁;否则,返回 false .

提供此函数是为兼容标准库概念 Lockable 。它相当于 tryLock ().

该函数在 Qt 5.8 引入。

[since 5.8] template <typename Rep, typename Period> bool QRecursiveMutex:: try_lock_for ( std::chrono::duration < Rep , Period > duration )

试图锁定互斥。此函数返回 true 若获得锁;否则,返回 false . If another thread has locked the mutex, this function will wait for at least duration for the mutex to become available.

Note: Passing a negative duration as the duration 相当于调用 try_lock (). This behavior differs from 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.

该函数在 Qt 5.8 引入。

另请参阅 lock () 和 unlock ().

[since 5.8] 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 for the mutex to become available.

注意:传递 timePoint which has already passed is equivalent to calling try_lock (). This behavior differs from 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.

该函数在 Qt 5.8 引入。

另请参阅 lock () 和 unlock ().

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