QtBase  v6.3.1
Public Types | Public Member Functions | List of all members
QReadWriteLock Class Reference

The QReadWriteLock class provides read-write locking. More...

#include <qreadwritelock.h>

Inheritance diagram for QReadWriteLock:
Inheritance graph
[legend]

Public Types

enum  RecursionMode { NonRecursive , Recursive }
 

Public Member Functions

 QReadWriteLock (RecursionMode=NonRecursive) noexcept
 
 ~QReadWriteLock ()
 
void lockForRead () noexcept
 
bool tryLockForRead () noexcept
 
bool tryLockForRead (int timeout) noexcept
 
void lockForWrite () noexcept
 
bool tryLockForWrite () noexcept
 
bool tryLockForWrite (int timeout) noexcept
 
void unlock () noexcept
 

Detailed Description

The QReadWriteLock class provides read-write locking.

\inmodule QtCore

\threadsafe

A read-write lock is a synchronization tool for protecting resources that can be accessed for reading and writing. This type of lock is useful if you want to allow multiple threads to have simultaneous read-only access, but as soon as one thread wants to write to the resource, all other threads must be blocked until the writing is complete.

In many cases, QReadWriteLock is a direct competitor to QMutex. QReadWriteLock is a good choice if there are many concurrent reads and writing occurs infrequently.

Example:

To ensure that writers aren't blocked forever by readers, readers attempting to obtain a lock will not succeed if there is a blocked writer waiting for access, even if the lock is currently only accessed by other readers. Also, if the lock is accessed by a writer and another writer comes in, that writer will have priority over any readers that might also be waiting.

Like QMutex, a QReadWriteLock can be recursively locked by the same thread when constructed with \l{QReadWriteLock::Recursive} as \l{QReadWriteLock::RecursionMode}. In such cases, unlock() must be called the same number of times lockForWrite() or lockForRead() was called. Note that the lock type cannot be changed when trying to lock recursively, i.e. it is not possible to lock for reading in a thread that already has locked for writing (and vice versa).

See also
QReadLocker, QWriteLocker, QMutex, QSemaphore

Definition at line 179 of file qreadwritelock.h.

Member Enumeration Documentation

◆ RecursionMode

Since
4.4

\value Recursive 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.

\value NonRecursive In this mode, a thread may only lock a QReadWriteLock once.

See also
QReadWriteLock()
Enumerator
NonRecursive 
Recursive 

Definition at line 182 of file qreadwritelock.h.

Constructor & Destructor Documentation

◆ QReadWriteLock()

QReadWriteLock::QReadWriteLock ( RecursionMode  recursionMode = NonRecursive)
inlineexplicitnoexcept
Since
4.4

Constructs a QReadWriteLock object in the given recursionMode.

The default recursion mode is NonRecursive.

See also
lockForRead(), lockForWrite(), RecursionMode

Definition at line 183 of file qreadwritelock.h.

◆ ~QReadWriteLock()

QReadWriteLock::~QReadWriteLock ( )
inline

Destroys the QReadWriteLock object.

Warning
Destroying a read-write lock that is in use may result in undefined behavior.

Definition at line 184 of file qreadwritelock.h.

Member Function Documentation

◆ lockForRead()

void QReadWriteLock::lockForRead ( )
inlinenoexcept

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(), tryLockForRead()

Definition at line 186 of file qreadwritelock.h.

Here is the caller graph for this function:

◆ lockForWrite()

void QReadWriteLock::lockForWrite ( )
inlinenoexcept

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 \l{QReadWriteLock::Recursive} mode).

It is not possible to lock for write if the thread already has locked for read.

See also
unlock(), lockForRead(), tryLockForWrite()

Definition at line 190 of file qreadwritelock.h.

Here is the caller graph for this function:

◆ tryLockForRead() [1/2]

bool QReadWriteLock::tryLockForRead ( )
inlinenoexcept

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(), lockForRead()

Definition at line 187 of file qreadwritelock.h.

◆ tryLockForRead() [2/2]

bool QReadWriteLock::tryLockForRead ( int  timeout)
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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(), lockForRead()

Definition at line 188 of file qreadwritelock.h.

Here is the call graph for this function:

◆ tryLockForWrite() [1/2]

bool QReadWriteLock::tryLockForWrite ( )
inlinenoexcept

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(), lockForWrite()

Definition at line 191 of file qreadwritelock.h.

◆ tryLockForWrite() [2/2]

bool QReadWriteLock::tryLockForWrite ( int  timeout)
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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(), lockForWrite()

Definition at line 192 of file qreadwritelock.h.

Here is the call graph for this function:

◆ unlock()

void QReadWriteLock::unlock ( )
inlinenoexcept

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(), tryLockForWrite()

Definition at line 194 of file qreadwritelock.h.

Here is the caller graph for this function:

The documentation for this class was generated from the following files: