QWaitCondition Class Reference

[QtCore module]

该QWaitCondition类提供了线程同步的条件变量。More…

Methods

  • __init__ (self)
  • bool wait (self, QMutex mutex, int msecs = ULONG_MAX)
  • bool wait (self, QReadWriteLock readWriteLock, int msecs = ULONG_MAX)
  • wakeAll (self)
  • wakeOne (self)

Detailed Description

该QWaitCondition类提供了线程同步的条件变量。

QWaitCondition允许一个线程告诉某种条件已经满足其他线程。一个或多个线程可以阻塞等待QWaitCondition设置的条件与wakeOne()或wakeAll( ) 。使用wakeOne( )来唤醒中随机选择的条件或wakeAll( )来唤醒他们。

例如,让我们假设我们有每当用户按下一个键应该执行三项任务。每个任务可以被分成一个线程,每一个都具有一run()身体是这样的:

  1. forever {
  2. mutex.lock();
  3. keyPressed.wait(&mutex);
  4. do_something();
  5. mutex.unlock();
  6. }

这里,本keyPressed变量的类型QWaitCondition的全局变量。

第四个线程将读取按键和每收到一个,比如这个时候唤醒其它三个线程了:

  1. forever {
  2. getchar();
  3. keyPressed.wakeAll();
  4. }

其中三个线程被唤醒的顺序是不确定的。此外,如果某些线程仍在do_something()当按键被按下时,他们不会被唤醒(因为他们没有等待条件变量) ,所以该任务将不会为该按键来执行。这个问题可以用一个计数器和一个可以解决QMutex守护它。例如,这里的工作线程新的代码:

  1. forever {
  2. mutex.lock();
  3. keyPressed.wait(&mutex);
  4. ++count;
  5. mutex.unlock();
  6. do_something();
  7. mutex.lock();
  8. --count;
  9. mutex.unlock();
  10. }

下面是第四个线程的代码:

  1. forever {
  2. getchar();
  3. mutex.lock();
  4. // Sleep until there are no busy worker threads
  5. while (count > 0) {
  6. mutex.unlock();
  7. sleep(1);
  8. mutex.lock();
  9. }
  10. keyPressed.wakeAll();
  11. mutex.unlock();
  12. }

互斥锁是必要的,因为两个线程试图改变同一个变量的值的结果同时是不可预知的。

等待条件是一个强大的线程同步原语。该Wait Conditions示例显示了如何使用QWaitCondition作为替代QSemaphore用于控制访问环形缓冲器由一个生产者线程和消费者线程共享。


Method Documentation

  1. QWaitCondition.__init__ (self)

构造一个新的等待条件的对象。

  1. bool QWaitCondition.wait (self, QMutex mutex, int msecs = ULONG_MAX)

释放锁定mutex并等待在等待条件。该mutex必须首先锁定调用线程。如果mutex是不是在锁定状态,该函数将立即返回。如果mutex是一个递归互斥体,该函数立即返回。该mutex将被解锁,并且调用线程将被阻塞,直到下列任一条件满足:

  • Another thread signals it using wakeOne() or wakeAll(). This function will return true in this case.
  • time milliseconds has elapsed. If time is ULONG_MAX (the default), then the wait will never timeout (the event must be signalled). This function will return false if the wait timed out.

互斥将被返回到相同的锁定状态。这个功能被提供,以允许从锁定状态到等待状态的原子跃迁。

See also wakeOne()和wakeAll( ) 。

  1. bool QWaitCondition.wait (self, QReadWriteLock readWriteLock, int msecs = ULONG_MAX)

释放锁定readWriteLock并等待在等待条件。该readWriteLock必须首先锁定调用线程。如果readWriteLock是不是在锁定状态,该函数将立即返回。该readWriteLock不能被递归锁定,否则此功能将无法正常释放该锁。该readWriteLock将被解锁,并且调用线程将被阻塞,直到下列任一条件满足:

  • Another thread signals it using wakeOne() or wakeAll(). This function will return true in this case.
  • time milliseconds has elapsed. If time is ULONG_MAX (the default), then the wait will never timeout (the event must be signalled). This function will return false if the wait timed out.

readWriteLock将返回到相同的锁定状态。这个功能被提供,以允许从锁定状态到等待状态的原子跃迁。

此功能被引入Qt的4.4 。

See also wakeOne()和wakeAll( ) 。

  1. QWaitCondition.wakeAll (self)

唤醒等待等待条件的所有线程。在该线程被唤醒的顺序依赖于操作系统的调度策略,并不能被控制或预测。

See also wakeOne( ) 。

  1. QWaitCondition.wakeOne (self)

唤醒等待的一个线程等待条件。该被唤醒的线程依赖于操作系统的调度策略,并且不能被控制或预测。

如果你想唤醒一个特定的线程,该解决方案通常使用不同的等待状态,并有不同的线程在等待不同的条件。

See also wakeAll( ) 。