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 () |
bool | tryLock (int timeout ) |
(从 6.6 起)
bool
|
tryLock (QDeadlineTimer 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 , 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:
另请参阅 QMutex , QMutexLocker , QReadWriteLock , QSemaphore ,和 QWaitCondition .
[constexpr noexcept]
QRecursiveMutex::
QRecursiveMutex
()
Constructs a new recursive mutex. The mutex is created in an unlocked state.
[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]
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.
[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 引入。
[noexcept]
bool
QRecursiveMutex::
try_lock
()
试图锁定互斥。此函数返回
true
若获得锁;否则,返回
false
.
提供此函数是为兼容标准库概念
Lockable
。它相当于
tryLock
().
试图锁定互斥。此函数返回
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.
试图锁定互斥。此函数返回
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.
[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 ().