|  | Home · All Classes · Main Classes · Annotated · Grouped Classes · Functions |

# QReadWriteLock Class Reference

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

    #include <QReadWriteLock>

Part of the QtCore module.

Note: All the functions in this class are thread-safe.

- List of all members, including inherited members

### Public Functions

- QReadWriteLock ()
- ~QReadWriteLock ()
- void lockForRead ()
- void lockForWrite ()
- bool tryLockForRead ()
- bool tryLockForWrite ()
- void unlock ()

## Detailed Description

The QReadWriteLock class provides read-write locking.

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:

        QReadWriteLock lock;

        void ReaderThread::run()
        {
            ...
            lock.lockForRead();
            read_file();
            lock.unlock();
            ...
        }

        void WriterThread::run()
        {
            ...
            lock.lockForWrite();
            write_file();
            lock.unlock();
            ...
        }

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.

See also QReadLocker, QWriteLocker, QMutex, and QSemaphore.

## Member Function Documentation

### QReadWriteLock::QReadWriteLock ()

Constructs a QReadWriteLock object.

See also lockForRead() and lockForWrite().

### QReadWriteLock::~QReadWriteLock ()

Destroys the QReadWriteLock object.

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

### void QReadWriteLock::lockForRead ()

Locks the lock for reading. This function will block the current thread if another thread has locked for writing.

See also unlock(), lockForWrite(), and tryLockForRead().

### void QReadWriteLock::lockForWrite ()

Locks the lock for writing. This function will block the current thread if any thread has locked for reading or writing.

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

### bool QReadWriteLock::tryLockForRead ()

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.

See also unlock() and lockForRead().

### bool QReadWriteLock::tryLockForWrite ()

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 any 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.

See also unlock() and lockForWrite().

### void QReadWriteLock::unlock ()

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