QReadLocker Class
QReadLocker 是便捷类, 它简化了对读写锁, 读访问的的锁定和解锁. More...
头文件: | #include <QReadLocker> |
qmake: | QT += core |
Note: All functions in this class are thread-safe.
公有函数
QReadLocker(QReadWriteLock *lock) | |
~QReadLocker() | |
QReadWriteLock * | readWriteLock() const |
void | relock() |
void | unlock() |
详细描述
QReadLocker (和 QWriteLocker) 的设计目的是简化 QReadWriteLock 的锁定和解锁. 锁定和解锁语句, 异常处理代码是很容易出错的, 而且很难调试. QReadLocker 可以确保在此类情况下, 锁的状态始终定义良好.
下面是一个使用 QReadLocker 锁定和解锁读写锁的示例:
QReadWriteLock lock; QByteArray readData() { QReadLocker locker(&lock); ... return data; }
等价于以下代码:
QReadWriteLock lock; QByteArray readData() { lock.lockForRead(); ... lock.unlock(); return data; }
QMutexLocker 文档展示了使用locker对象来大大简化编程的示例.
另见 QWriteLocker 和 QReadWriteLock.
Member Function Documentation
QReadLocker::QReadLocker(QReadWriteLock *lock)
Constructs a QReadLocker and locks lock for reading. The lock will be unlocked when the QReadLocker is destroyed. If lock
is zero, QReadLocker does nothing.
See also QReadWriteLock::lockForRead().
QReadLocker::~QReadLocker()
Destroys the QReadLocker and unlocks the lock that was passed to the constructor.
See also QReadWriteLock::unlock().
QReadWriteLock *QReadLocker::readWriteLock() const
Returns a pointer to the read-write lock that was passed to the constructor.
void QReadLocker::relock()
Relocks an unlocked lock.
See also unlock().
void QReadLocker::unlock()
Unlocks the lock associated with this locker.
See also QReadWriteLock::unlock().