QReadWriteLock Class
QReadWriteLock 提供读写锁定. More...
头文件: | #include <QReadWriteLock> |
qmake: | QT += core |
Note: All functions in this class are thread-safe.
公有类型
enum | RecursionMode { Recursive, NonRecursive } |
公有函数
QReadWriteLock(QReadWriteLock::RecursionMode recursionMode = NonRecursive) | |
~QReadWriteLock() | |
void | lockForRead() |
void | lockForWrite() |
bool | tryLockForRead() |
bool | tryLockForRead(int timeout) |
bool | tryLockForWrite() |
bool | tryLockForWrite(int timeout) |
void | unlock() |
详细描述
读写锁是一种用于保护读写资源的同步工具. 如果希望允许多个线程同时进行只读访问, 那么这种类型的锁很有用, 但是只要一个线程想写入资源, 那么它就必须阻止所有其他线程, 直到写入完成.
在许多情况下, QReadWriteLock 是 QMutex 强有力的竞争对手. QReadWriteLock 在多并发读, 少量写的情况下, 有较好的执行效果.
Example:
QReadWriteLock lock; void ReaderThread::run() { ... lock.lockForRead(); read_file(); lock.unlock(); ... } void WriterThread::run() { ... lock.lockForWrite(); write_file(); lock.unlock(); ... }
为了确保写者永远不会被读者阻塞, 如果有写者被阻塞, 则读者将无法获取锁, 即使该锁当前正被其他读者访问. 另外, 如果一个写者正在写, 而另一个写者想进入, 则该写者将优先于其他可能正在等待的读者.
与 QMutex 类似, 当使用 QReadWriteLock::Recursive 作为 QReadWriteLock::RecursionMode 参数构造时, QReadWriteLock 可以被同一线程递归锁定. 在这种情况下, unlock() 的调用次数必须与 lockForWrite() 或 lockForRead() 的调用次数相同. 注意, 当尝试递归锁定时, 不能更改锁类型, 也就是说, 在已经为写入而锁定的线程中, 不可能为读取而锁定 (反之亦然).
参见 QReadLocker, QWriteLocker, QMutex, QSemaphore.
Member Type Documentation
enum QReadWriteLock::RecursionMode
Constant | Value | Description |
---|---|---|
QReadWriteLock::Recursive | 1 | In this mode, a thread can lock the same QReadWriteLock multiple times. The QReadWriteLock won't be unlocked until a corresponding number of unlock() calls have been made. |
QReadWriteLock::NonRecursive | 0 | In this mode, a thread may only lock a QReadWriteLock once. |
This enum was introduced or modified in Qt 4.4.
See also QReadWriteLock().
Member Function Documentation
QReadWriteLock::QReadWriteLock(QReadWriteLock::RecursionMode recursionMode = NonRecursive)
Constructs a QReadWriteLock object in the given recursionMode.
The default recursion mode is NonRecursive.
This function was introduced in Qt 4.4.
See also lockForRead(), lockForWrite(), and RecursionMode.
QReadWriteLock::~QReadWriteLock()
Destroys the QReadWriteLock object.
Warning: Destroying a read-write lock that is in use may result in undefined behavior.
void QReadWriteLock::lockForRead()
Locks the lock for reading. This function will block the current thread if another thread has locked for writing.
It is not possible to lock for read if the thread already has locked for write.
See also unlock(), lockForWrite(), and tryLockForRead().
void QReadWriteLock::lockForWrite()
Locks the lock for writing. This function will block the current thread if another thread (including the current) has locked for reading or writing (unless the lock has been created using the QReadWriteLock::Recursive mode).
It is not possible to lock for write if the thread already has locked for read.
See also unlock(), lockForRead(), and tryLockForWrite().
bool QReadWriteLock::tryLockForRead()
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.
The lock attempt will fail if another thread has locked for writing.
If the lock was obtained, the lock must be unlocked with unlock() before another thread can successfully lock it for writing.
It is not possible to lock for read if the thread already has locked for write.
See also unlock() and lockForRead().
bool QReadWriteLock::tryLockForRead(int timeout)
This is an overloaded function.
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 milliseconds for the lock to become available.
Note: Passing a negative number as the timeout is equivalent to calling lockForRead(), i.e. this function will wait forever until lock can be locked for reading when timeout is negative.
If the lock was obtained, the lock must be unlocked with unlock() before another thread can successfully lock it for writing.
It is not possible to lock for read if the thread already has locked for write.
See also unlock() and lockForRead().
bool QReadWriteLock::tryLockForWrite()
Attempts to lock for writing. If the lock was obtained, this function returns true
; otherwise, it returns false
immediately.
The lock attempt will fail if another thread has locked for reading or writing.
If the lock was obtained, the lock must be unlocked with unlock() before another thread can successfully lock it.
It is not possible to lock for write if the thread already has locked for read.
See also unlock() and lockForWrite().
bool QReadWriteLock::tryLockForWrite(int timeout)
This is an overloaded function.
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 milliseconds for the lock to become available.
Note: Passing a negative number as the timeout is equivalent to calling lockForWrite(), i.e. this function will wait forever until lock can be locked for writing when timeout is negative.
If the lock was obtained, the lock must be unlocked with unlock() before another thread can successfully lock it.
It is not possible to lock for write if the thread already has locked for read.
See also unlock() and lockForWrite().
void QReadWriteLock::unlock()
Unlocks the lock.
Attempting to unlock a lock that is not locked is an error, and will result in program termination.
See also lockForRead(), lockForWrite(), tryLockForRead(), and tryLockForWrite().