QReadLocker 類

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

頭: #include <QReadLocker>
qmake: QT += core

注意: 此類的所有函數 綫程安全 .

公共函數

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 ().