QWaitConditionclass provides a condition variable for synchronizing threads. 更多 …
def
notify_all
()
def
notify_one
()
def
wait
(lockedMutex, time)
def
wait
(lockedMutex[, deadline=QDeadlineTimer(QDeadlineTimer.Forever)])
def
wait
(lockedReadWriteLock, time)
def
wait
(lockedReadWriteLock[, deadline=QDeadlineTimer(QDeadlineTimer.Forever)])
def
wakeAll
()
def
wakeOne
()
QWaitConditionallows a thread to tell other threads that some sort of condition has been met. One or many threads can block waiting for aQWaitConditionto set a condition withwakeOne()orwakeAll()。使用wakeOne()to wake one randomly selected thread orwakeAll()to wake them all.For example, let’s suppose that we have three tasks that should be performed whenever the user presses a key. Each task could be split into a thread, each of which would have a
run()本体像这样:while True: mutex.lock() keyPressed.wait(mutex) do_something() mutex.unlock()在这里,
keyPressedvariable is a global variable of typeQWaitCondition.第 4 个线程将读取键按下,并在每次收到一个时唤醒其他 3 个线程,像这样:
while True: getchar() keyPressed.wakeAll()3 个线程被唤醒的次序不确定。另外,若某些线程仍在
do_something()when the key is pressed, they won’t be woken up (since they’re not waiting on the condition variable) and so the task will not be performed for that key press. This issue can be solved using a counter and aQMutexto guard it. For example, here’s the new code for the worker threads:while True: mutex.lock() keyPressed.wait(&mutex) count += 1 mutex.unlock() do_something() mutex.lock() count -= 1 mutex.unlock()Here’s the code for the fourth thread:
while True: getchar() mutex.lock() # Sleep until there are no busy worker threads while count > 0: mutex.unlock() sleep(1) mutex.lock() keyPressed.wakeAll() mutex.unlock()互斥是必要的,因为试图同时改变同一变量值的 2 个线程的结果是不可预测的。
等待条件是强大的线程同步原语。 等待条件范例 example shows how to use
QWaitConditionas an alternative toQSemaphore为控制由生产者线程和消费者线程共享的循环缓冲的访问。另请参阅
QWaitCondition
¶
构造新等待条件对象。
PySide2.QtCore.QWaitCondition.
wait
(
lockedMutex
[
,
deadline=QDeadlineTimer(QDeadlineTimer.Forever)
]
)
¶
lockedMutex
–
QMutex
deadline
–
QDeadlineTimer
bool
释放
lockedMutex
并等待等待条件。
lockedMutex
must be initially locked by the calling thread. If
lockedMutex
is not in a locked state, the behavior is undefined. If
lockedMutex
is a recursive mutex, this function returns immediately. The
lockedMutex
will be unlocked, and the calling thread will block until either of these conditions is met:
Another thread signals it using
wakeOne()
or
wakeAll()
. This function will return true in this case.
the deadline given by
deadline
is reached. If
deadline
is
QDeadlineTimer::Forever
(the default), then the wait will never timeout (the event must be signalled). This function will return false if the wait timed out.
lockedMutex
will be returned to the same locked state. This function is provided to allow the atomic transition from the locked state to the wait state.
PySide2.QtCore.QWaitCondition.
wait
(
lockedMutex
,
time
)
¶
lockedMutex
–
QMutex
time – long
bool
这是重载函数。
PySide2.QtCore.QWaitCondition.
wait
(
lockedReadWriteLock
[
,
deadline=QDeadlineTimer(QDeadlineTimer.Forever)
]
)
¶
lockedReadWriteLock
–
QReadWriteLock
deadline
–
QDeadlineTimer
bool
释放
lockedReadWriteLock
并等待等待条件。
lockedReadWriteLock
must be initially locked by the calling thread. If
lockedReadWriteLock
is not in a locked state, this function returns immediately. The
lockedReadWriteLock
must not be locked recursively, otherwise this function will not release the lock properly. The
lockedReadWriteLock
will be unlocked, and the calling thread will block until either of these conditions is met:
Another thread signals it using
wakeOne()
or
wakeAll()
. This function will return true in this case.
the deadline given by
deadline
is reached. If
deadline
is
QDeadlineTimer::Forever
(the default), then the wait will never timeout (the event must be signalled). This function will return false if the wait timed out.
lockedReadWriteLock
will be returned to the same locked state. This function is provided to allow the atomic transition from the locked state to the wait state.
PySide2.QtCore.QWaitCondition.
wait
(
lockedReadWriteLock
,
time
)
¶
lockedReadWriteLock
–
QReadWriteLock
time – long
bool
这是重载函数。
PySide2.QtCore.QWaitCondition.
wakeAll
(
)
¶
Wakes all threads waiting on the wait condition. The order in which the threads are woken up depends on the operating system’s scheduling policies and cannot be controlled or predicted.
另请参阅
PySide2.QtCore.QWaitCondition.
wakeOne
(
)
¶
Wakes one thread waiting on the wait condition. The thread that is woken up depends on the operating system’s scheduling policies, and cannot be controlled or predicted.
If you want to wake up a specific thread, the solution is typically to use different wait conditions and have different threads wait on different conditions.
另请参阅