Table Of Contents

Previous topic

QReadLocker

Next topic

QMutexLocker

QReadWriteLock

Synopsis

Functions

Detailed Description

The PySide.QtCore.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, PySide.QtCore.QReadWriteLock is a direct competitor to PySide.QtCore.QMutex . PySide.QtCore.QReadWriteLock is a good choice if there are many concurrent reads and writing occurs infrequently.

Example:

lock = QReadWriteLock()

class ReaderThread:
    # ...
    def run():
        # ...
        lock.lockForRead()
        read_file()
        lock.unlock()
        # ...

class WriterThread:
    #...
    def 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.

Like PySide.QtCore.QMutex , a PySide.QtCore.QReadWriteLock can be recursively locked by the same thread when constructed in QReadWriteLock.RecursionMode . In such cases, PySide.QtCore.QReadWriteLock.unlock() must be called the same number of times PySide.QtCore.QReadWriteLock.lockForWrite() or PySide.QtCore.QReadWriteLock.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).

class PySide.QtCore.QReadWriteLock
class PySide.QtCore.QReadWriteLock(recursionMode)
Parameters:recursionModePySide.QtCore.QReadWriteLock.RecursionMode

Constructs a PySide.QtCore.QReadWriteLock object in NonRecursive mode.

Constructs a PySide.QtCore.QReadWriteLock object in the given recursionMode .

PySide.QtCore.QReadWriteLock.RecursionMode
Constant Description
QReadWriteLock.Recursive In this mode, a thread can lock the same PySide.QtCore.QReadWriteLock multiple times and the mutex won’t be unlocked until a corresponding number of PySide.QtCore.QReadWriteLock.unlock() calls have been made.
QReadWriteLock.NonRecursive In this mode, a thread may only lock a PySide.QtCore.QReadWriteLock once.

See also

PySide.QtCore.QReadWriteLock.QReadWriteLock()

PySide.QtCore.QReadWriteLock.lockForRead()

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

PySide.QtCore.QReadWriteLock.lockForWrite()

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

PySide.QtCore.QReadWriteLock.tryLockForRead()
Return type:PySide.QtCore.bool

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 PySide.QtCore.QReadWriteLock.unlock() before another thread can successfully lock it.

PySide.QtCore.QReadWriteLock.tryLockForRead(timeout)
Parameters:timeoutPySide.QtCore.int
Return type:PySide.QtCore.bool

This is an overloaded function.

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 PySide.QtCore.QReadWriteLock.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 PySide.QtCore.QReadWriteLock.unlock() before another thread can successfully lock it.

PySide.QtCore.QReadWriteLock.tryLockForWrite(timeout)
Parameters:timeoutPySide.QtCore.int
Return type:PySide.QtCore.bool

This is an overloaded function.

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 PySide.QtCore.QReadWriteLock.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 PySide.QtCore.QReadWriteLock.unlock() before another thread can successfully lock it.

PySide.QtCore.QReadWriteLock.tryLockForWrite()
Return type:PySide.QtCore.bool

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 PySide.QtCore.QReadWriteLock.unlock() before another thread can successfully lock it.

PySide.QtCore.QReadWriteLock.unlock()

Unlocks the lock.

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