The QReadWriteLock 类提供读/写锁定。 更多...
| 头: | #include <QReadWriteLock> |
注意: 此类的所有函数 thread-safe .
| enum | RecursionMode { Recursive, NonRecursive } |
| QReadWriteLock () | |
| QReadWriteLock (RecursionMode recursionMode ) | |
| ~QReadWriteLock () | |
| void | lockForRead () |
| void | lockForWrite () |
| bool | tryLockForRead () |
| bool | tryLockForRead (int timeout ) |
| bool | tryLockForWrite () |
| bool | tryLockForWrite (int timeout ) |
| void | unlock () |
The QReadWriteLock 类提供读/写锁定。
读/写锁是用于保护可供读/写访问的资源的同步工具。这种类型的锁很有用,若希望允许多个线程同时进行只读访问,但一旦某一线程想要写入资源就必须阻塞所有其它线程,直到写入完成。
在很多情况下, QReadWriteLock 的直接竞争者是 QMutex . QReadWriteLock 是不错选择,若有许多并发读取但不经常发生写入。
范例:
QReadWriteLock lock; void ReaderThread::run() { ... lock.lockForRead(); read_file(); lock.unlock(); ... } void WriterThread::run() { ... lock.lockForWrite(); write_file(); lock.unlock(); ... }
为确保读取器不永远阻塞写入器,试图获取锁的读取器不会成功若存在等待访问的阻塞写入器,即使目前只有其它读取器在访问锁。此外,若写入器在访问锁,而另一写入器进入,则该写入器将优先于可能也在等待的任何读取器。
像 QMutex , QReadWriteLock can be recursively locked by the same thread when constructed in QReadWriteLock::RecursionMode 。在这种情况下, unlock () 必须调用相同次数如 lockForWrite () 或 lockForRead () 的调用。注意,无法改变锁类型当试着递归锁定时,即:在已锁定写入的线程中锁定读取是不可能的 (反之亦然)。
另请参阅 QReadLocker , QWriteLocker , QMutex ,和 QSemaphore .
| 常量 | 值 | 描述 |
|---|---|---|
QReadWriteLock::Recursive
|
1
|
在此模式下,线程可以锁定同一 QReadWriteLock multiple times and the mutex won't be unlocked until a corresponding number of unlock () 调用已做出。 |
QReadWriteLock::NonRecursive
|
0
|
在此模式下,线程只能锁定 QReadWriteLock 一次。 |
该枚举在 Qt 4.4 引入或被修改。
另请参阅 QReadWriteLock ().
构造 QReadWriteLock object in NonRecursive 模式。
另请参阅 lockForRead () 和 lockForWrite ().
构造 QReadWriteLock 对象以给定 recursionMode .
该函数在 Qt 4.4 引入。
另请参阅 lockForRead (), lockForWrite (),和 RecursionMode .
销毁 QReadWriteLock 对象。
警告: 销毁使用中的读/写锁可能导致未定义行为。
Locks the lock for reading. This function will block the current thread if any thread (including the current) has locked for writing.
另请参阅 unlock (), lockForWrite (),和 tryLockForRead ().
Locks the lock for writing. This function will block the current thread if another thread has locked for reading or writing.
另请参阅 unlock (), lockForRead (),和 tryLockForWrite ().
Attempts to lock for reading. If the lock was obtained, this function returns true, otherwise it returns false instead of waiting for the lock to become available, i.e. it does not block.
锁定尝试将失败,若另一线程已锁定写入。
若获得锁,锁必须被解锁采用 unlock () 在另一线程可以成功锁定它之前。
另请参阅 unlock () 和 lockForRead ().
这是重载函数。
Attempts to lock for reading. This function returns true if the lock was obtained; otherwise it returns false. If another thread has locked for writing, this function will wait for at most timeout 毫秒为锁变为可用。
注意:传递负数作为 timeout 相当于调用 lockForRead (),即,此函数将永远等待直到锁可以锁定读取为此当 timeout 为负。
若获得锁,锁必须被解锁采用 unlock () 在另一线程可以成功锁定它之前。
另请参阅 unlock () 和 lockForRead ().
Attempts to lock for writing. If the lock was obtained, this function returns true; otherwise, it returns false immediately.
锁定尝试将失败,若另一线程已锁定读取或写入。
若获得锁,锁必须被解锁采用 unlock () 在另一线程可以成功锁定它之前。
另请参阅 unlock () 和 lockForWrite ().
这是重载函数。
Attempts to lock for writing. This function returns true if the lock was obtained; otherwise it returns false. If another thread has locked for reading or writing, this function will wait for at most timeout 毫秒为锁变为可用。
注意:传递负数作为 timeout 相当于调用 lockForWrite (),即,此函数将永远等待直到锁可以锁定写入为此当 timeout 为负。
若获得锁,锁必须被解锁采用 unlock () 在另一线程可以成功锁定它之前。
另请参阅 unlock () 和 lockForWrite ().
解锁锁。
试图解锁未锁定的锁是错误的,并将导致程序终止。
另请参阅 lockForRead (), lockForWrite (), tryLockForRead (),和 tryLockForWrite ().