QReadWriteLockclass provides read-write locking. 更多 …
def
lockForRead
()
def
lockForWrite
()
def
tryLockForRead
()
def
tryLockForRead
(timeout)
def
tryLockForWrite
()
def
tryLockForWrite
(timeout)
def
unlock
()
读写锁是用于保护可以读写访问的资源的同步工具。这种类型的锁很有用,若希望允许多个线程同时进行只读访问,但一旦某一线程想要写入资源就必须阻塞所有其它线程,直到写入完成。
In many cases,
QReadWriteLockis a direct competitor toQMutex.QReadWriteLockis a good choice if there are many concurrent reads and writing occurs infrequently.范例:
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.
像
QMutex,QReadWriteLockcan 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).
QReadWriteLock
(
[
recursionMode=NonRecursive
]
)
¶
- param recursionMode
RecursionMode
构造
QReadWriteLock
object in the given
recursionMode
.
The default recursion mode is
NonRecursive
.
另请参阅
lockForRead()
lockForWrite()
RecursionMode
PySide2.QtCore.QReadWriteLock.
RecursionMode
¶
|
常量 |
描述 |
|---|---|
|
QReadWriteLock.Recursive |
In this mode, a thread can lock the same
|
|
QReadWriteLock.NonRecursive |
In this mode, a thread may only lock a
|
另请参阅
QReadWriteLock()
PySide2.QtCore.QReadWriteLock.
StateForWaitCondition
¶
PySide2.QtCore.QReadWriteLock.
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.
PySide2.QtCore.QReadWriteLock.
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
Recursive
mode).
It is not possible to lock for write if the thread already has locked for read.
PySide2.QtCore.QReadWriteLock.
tryLockForRead
(
)
¶
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
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.
另请参阅
PySide2.QtCore.QReadWriteLock.
tryLockForRead
(
timeout
)
¶
timeout
–
int
bool
这是重载函数。
Attempts to lock for reading. This function returns
true
若获得锁;否则它返回
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.
另请参阅
PySide2.QtCore.QReadWriteLock.
tryLockForWrite
(
)
¶
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
unlock()
before another thread can successfully lock it.
It is not possible to lock for write if the thread already has locked for read.
另请参阅
PySide2.QtCore.QReadWriteLock.
tryLockForWrite
(
timeout
)
¶
timeout
–
int
bool
这是重载函数。
Attempts to lock for writing. This function returns
true
若获得锁;否则它返回
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.
另请参阅
PySide2.QtCore.QReadWriteLock.
unlock
(
)
¶
Unlocks the lock.
Attempting to unlock a lock that is not locked is an error, and will result in program termination.