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

# QReadWriteLock Class Reference

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

    #include <QReadWriteLock>

This class is under development and is subject to change.

- List of all members, including inherited members

### Public Types

- enum AccessMode { ReadAccess, WriteAccess }

### Public Functions

- QReadWriteLock ( const int maxReaders = INT_MAX )
- ~QReadWriteLock ()
- void lock ( AccessMode mode )
- bool tryLock ( AccessMode mode )
- void unlock ()

## Detailed Description

The QReadWriteLock class provides read-write locking.

It is useful for synchronizing multithreaded access to resources that support multiple readers, but only one writer.

The lock is optimized for situations where there are many concurrent reads, and writing occurs infrequently. Writers are prioritized in front of readers, in the sense that readers attemting to optain the lock will yield to writers attemting to optain the lock.

## Member Type Documentation

### enum QReadWriteLock::AccessMode

This enum describes the different access types that a thread can specify when requesting the mutex.

`QReadWriteLock::ReadAccess` | The thread wants to perform read-operations.
`QReadWriteLock::WriteAccess` | The thread wants to perform write-operations.

## Member Function Documentation

### QReadWriteLock::QReadWriteLock ( const int maxReaders = INT_MAX )

Constructs a QReadWriteLock object allowing maxReaders concurrent readers. The default is INT_MAX.

See also lock().

### QReadWriteLock::~QReadWriteLock ()

Destroys the QReadWriteMutex object.

Warning: If you destroy a read/write mutex that has accesses in use the resultant behavior is undefined.

### void QReadWriteLock::lock ( AccessMode mode )

Attempts to lock the lock. This function will block the current thread if

- access is ReadAccess and one thread is writing.
- access is WriteAccess and any thread is already reading or writing.

See also unlock(), tryLock(), and ;.

### bool QReadWriteLock::tryLock ( AccessMode mode )

Attempt to lock the lock. 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 one of the following is true:

- mode is ReadAccess and one thread is writing.
- mode is WriteAccess and any thread is already 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(), lock(), and ;.

### void QReadWriteLock::unlock ()

Unlocks the lock.

Attempting to unlock a lock that i not locked is an error and will result in program termination.

See also lock(), trylock(), and ;.