QReadLocker 类

The QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks for read access. 更多...

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

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

公共函数

QReadLocker (QReadWriteLock * lock )
~QReadLocker ()
QReadWriteLock * readWriteLock () const
void relock ()
void unlock ()

详细描述

The purpose of QReadLocker (and QWriteLocker ) 是简化 QReadWriteLock locking and unlocking. Locking and unlocking statements or in exception handling code is error-prone and difficult to debug. QReadLocker can be used in such situations to ensure that the state of the lock is always well-defined.

Here's an example that uses QReadLocker to lock and unlock a read-write lock for reading:

QReadWriteLock lock;
QByteArray readData()
{
    QReadLocker locker(&lock);
    ...
    return data;
}
					

它相当于以下代码:

QReadWriteLock lock;
QByteArray readData()
{
    lock.lockForRead();
    ...
    lock.unlock();
    return data;
}
					

The QMutexLocker 文档编制展示使用锁定器对象大大简化编程的范例。

另请参阅 QWriteLocker and QReadWriteLock .

成员函数文档编制

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.

另请参阅 QReadWriteLock::lockForRead ().

QReadLocker:: ~QReadLocker ()

销毁 QReadLocker 并解锁被传递给构造函数的锁。

另请参阅 QReadWriteLock::unlock ().

QReadWriteLock *QReadLocker:: readWriteLock () const

返回指向被传递给构造函数的读写锁的指针。

void QReadLocker:: relock ()

重新锁定被解锁的锁。

另请参阅 unlock ().

void QReadLocker:: unlock ()

解锁关联此锁定器的锁。

另请参阅 QReadWriteLock::unlock ().