6.0.0-rc4Source

QReadWriteLock Class

The QReadWriteLock class provides read-write locking. More…

Header#include <QReadWriteLock>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QReadWriteLock(RecursionMode recursionMode = NonRecursive)
voidlockForRead()
voidlockForWrite()
booltryLockForRead(int timeoutMs = 0)
booltryLockForRead(QDeadlineTimer deadline)
booltryLockForWrite(int timeoutMs = 0)
booltryLockForWrite(QDeadlineTimer deadline)
voidunlock()

Detailed Description

A QReadWriteLock lets many threads read shared data at the same time while giving a writing thread exclusive access. It suits data that is read often and written rarely.

QReadWriteLock lock;

void reader()
{
    QReadLocker locker(&lock);
    // read shared data
}

void writer()
{
    QWriteLocker locker(&lock);
    // modify shared data
}

Writers are preferred: once a thread is waiting to lock for writing, new readers block until it has finished.

A lock constructed with QReadWriteLock::Recursive can be locked several times by the thread that holds it, in the mode in which it holds it, and is released by the same number of calls to unlock(). A thread that holds such a lock for writing can also lock it for reading. No lock can be upgraded: a thread that holds a lock for reading and locks it for writing waits for itself. A thread that holds a non-recursive lock for reading must not lock it for reading again, because that call blocks while a writer waits.

See also QReadLocker, QWriteLocker and QMutex.

Differences from Qt

None.

Member Type Documentation

enum QReadWriteLock::RecursionMode

This enum says whether a thread can lock a lock that it already holds.

ConstantDescription
QReadWriteLock::NonRecursiveA thread locks the lock once.
QReadWriteLock::RecursiveA thread can lock the lock several times in the same mode, and releases it with as many calls to unlock().

This function was introduced in 4.4.

Member Function Documentation

[explicit] QReadWriteLock::QReadWriteLock(RecursionMode recursionMode = NonRecursive)

Constructs an unlocked read-write lock with the given recursionMode. The default is a non-recursive lock.

This function was introduced in 4.4.

void QReadWriteLock::lockForRead()

Locks the lock for reading. This call blocks while another thread holds the lock for writing or is waiting to. A thread that already holds a recursive lock gets it at once.

See also tryLockForRead() and unlock().

void QReadWriteLock::lockForWrite()

Locks the lock for writing. This call blocks until no other thread holds the lock for reading or writing. A thread that already holds a recursive lock for writing gets it at once.

See also tryLockForWrite() and unlock().

bool QReadWriteLock::tryLockForRead(int timeoutMs = 0)

Attempts to lock the lock for reading, waiting up to timeoutMs milliseconds. Returns true if the lock was obtained; otherwise returns false. If timeoutMs is 0, the call returns immediately; if it is negative, the call waits like lockForRead().

See also lockForRead() and unlock().

bool QReadWriteLock::tryLockForRead(QDeadlineTimer deadline)

This is an overloaded function.

Attempts to lock the lock for reading until deadline expires. Returns true if the lock was obtained; otherwise returns false.

This function was introduced in 6.6.

bool QReadWriteLock::tryLockForWrite(int timeoutMs = 0)

Attempts to lock the lock for writing, waiting up to timeoutMs milliseconds. Returns true if the lock was obtained; otherwise returns false. If timeoutMs is 0, the call returns immediately; if it is negative, the call waits like lockForWrite().

See also lockForWrite() and unlock().

bool QReadWriteLock::tryLockForWrite(QDeadlineTimer deadline)

This is an overloaded function.

Attempts to lock the lock for writing until deadline expires. Returns true if the lock was obtained; otherwise returns false.

This function was introduced in 6.6.

void QReadWriteLock::unlock()

Unlocks the lock. The calling thread must hold it for reading or for writing; if the lock is held for writing, the write lock is released, otherwise one read lock is. A recursive lock that the thread locked several times is released by the last matching call.

See also lockForRead() and lockForWrite().