PySide6.QtCore.QReadWriteLock¶
- class QReadWriteLock¶
The
QReadWriteLockclass provides read-write locking.Details
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
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,
QReadWriteLockis a direct competitor toQMutex.QReadWriteLockis a good choice if there are many concurrent reads and writing occurs infrequently.Example:
lock = QReadWriteLock() def run(self): ... lock.lockForRead() read_file() lock.unlock() ... def run(self): ... 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
QMutex, aQReadWriteLockcan be recursively locked by the same thread when constructed withRecursiveasRecursionMode. In such cases,unlock()must be called the same number of timeslockForWrite()orlockForRead()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
Synopsis¶
Methods¶
def
__init__()def
lockForRead()def
lockForWrite()def
tryLockForRead()def
unlock()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
- class RecursionMode¶
Constant
Description
QReadWriteLock.RecursionMode.Recursive
In this mode, a thread can lock the same
QReadWriteLockmultiple times. TheQReadWriteLockwon’t be unlocked until a corresponding number ofunlock()calls have been made.QReadWriteLock.RecursionMode.NonRecursive
In this mode, a thread may only lock a
QReadWriteLockonce.See also
QReadWriteLock()
- __init__([recursionMode=QReadWriteLock.RecursionMode.NonRecursive])¶
- Parameters:
recursionMode –
RecursionMode
Constructs a
QReadWriteLockobject in the givenrecursionMode.The default recursion mode is
NonRecursive.See also
- lockForRead()¶
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
- lockForWrite()¶
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
Recursivemode).It is not possible to lock for write if the thread already has locked for read.
See also
- tryLockForRead([timeout={}])¶
- Parameters:
timeout –
QDeadlineTimer- Return type:
bool
Attempts to lock for reading. This function returns
trueif the lock was obtained; otherwise it returnsfalse. If another thread has locked for writing, this function will wait untiltimeoutexpires for the lock to become available.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
- tryLockForRead(timeout)
- Parameters:
timeout – int
- Return type:
bool
Attempts to lock for reading. This function returns
trueif the lock was obtained; otherwise it returnsfalse. If another thread has locked for writing, this function will wait for at mosttimeoutmilliseconds for the lock to become available.Note: Passing a negative number as the
timeoutis equivalent to callinglockForRead(), i.e. this function will wait forever until lock can be locked for reading whentimeoutis 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
- tryLockForWrite([timeout={}])¶
- Parameters:
timeout –
QDeadlineTimer- Return type:
bool
Attempts to lock for writing. This function returns
trueif the lock was obtained; otherwise it returnsfalse. If another thread has locked for reading or writing, this function will wait untiltimeoutexpires for the lock to become available.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
- tryLockForWrite(timeout)
- Parameters:
timeout – int
- Return type:
bool
Attempts to lock for writing. This function returns
trueif the lock was obtained; otherwise it returnsfalse. If another thread has locked for reading or writing, this function will wait for at mosttimeoutmilliseconds for the lock to become available.Note: Passing a negative number as the
timeoutis equivalent to callinglockForWrite(), i.e. this function will wait forever until lock can be locked for writing whentimeoutis 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()¶
Unlocks the lock.
Attempting to unlock a lock that is not locked is an error, and will result in program termination.