QWaitCondition类官方手册
来源:互联网 发布:js数组push remove 编辑:程序博客网 时间:2024/06/07 15:19
The QWaitCondition class provides a condition variable for synchronizing threads.
QWaitCondition allows a thread to tell other threads that some sort of condition has been met. One or many threads can block waiting for a QWaitCondition to set a condition with wakeOne() or wakeAll(). Use wakeOne() to wake one randomly selected thread or wakeAll() 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() body like this:
forever {
mutex.lock();
keyPressed.wait(&mutex);
do_something();
mutex.unlock();
}
Here, the keyPressed variable is a global variable of type QWaitCondition.
A fourth thread would read key presses and wake the other three threads up every time it receives one, like this:
forever {
getchar();
keyPressed.wakeAll();
}
The order in which the three threads are woken up is undefined. Also, if some of the threads are still in 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 a QMutex to guard it. For example, here's the new code for the worker threads:
forever {
mutex.lock();
keyPressed.wait(&mutex);
++count;
mutex.unlock();
do_something();
mutex.lock();
--count;
mutex.unlock();
}
Here's the code for the fourth thread:
forever {
getchar();
mutex.lock();
// Sleep until there are no busy worker threads
while (count > 0) {
mutex.unlock();
sleep(1);
mutex.lock();
}
keyPressed.wakeAll();
mutex.unlock();
}
The mutex is necessary because the results of two threads attempting to change the value of the same variable simultaneously are unpredictable.
QWaitCondition allows a thread to tell other threads that some sort of condition has been met. One or many threads can block waiting for a QWaitCondition to set a condition with wakeOne() or wakeAll(). Use wakeOne() to wake one randomly selected thread or wakeAll() 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() body like this:
forever {
mutex.lock();
keyPressed.wait(&mutex);
do_something();
mutex.unlock();
}
Here, the keyPressed variable is a global variable of type QWaitCondition.
A fourth thread would read key presses and wake the other three threads up every time it receives one, like this:
forever {
getchar();
keyPressed.wakeAll();
}
The order in which the three threads are woken up is undefined. Also, if some of the threads are still in 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 a QMutex to guard it. For example, here's the new code for the worker threads:
forever {
mutex.lock();
keyPressed.wait(&mutex);
++count;
mutex.unlock();
do_something();
mutex.lock();
--count;
mutex.unlock();
}
Here's the code for the fourth thread:
forever {
getchar();
mutex.lock();
// Sleep until there are no busy worker threads
while (count > 0) {
mutex.unlock();
sleep(1);
mutex.lock();
}
keyPressed.wakeAll();
mutex.unlock();
}
The mutex is necessary because the results of two threads attempting to change the value of the same variable simultaneously are unpredictable.
Wait conditions are a powerful thread synchronization primitive. The Wait Conditions Example example shows how to use QWaitCondition as an alternative to QSemaphore for controlling access to a circular buffer shared by a producer thread and a consumer thread.
0 0
- QWaitCondition类官方手册
- QWaitCondition类
- QWaitCondition
- QWaitCondition
- mod_python官方手册翻译
- XPath官方手册中文版
- PhysX官方手册翻译
- vim官方中文版手册
- emacs官方中文手册
- Twig官方手册
- velocity 官方手册
- PhysX官方手册翻译
- boost官方手册
- 官方手册集锦
- docker官方手册
- iptables官方手册整理
- OpenWrt官方入门手册
- sed 手册官方位置
- RevealiOS使用教程之简易配置:不修改您的Xcode工程并加载Reveal
- 对话框的动画以及去掉对话框的标题
- Codeforces Round #339 D. Skills
- SQL.WITH AS.公用表表达式(CTE)
- JavaScript中,for, while, if, switch用到的后缀递减(a--)运算
- QWaitCondition类官方手册
- android桌面小组件(时钟)--widget初体验
- Java多线程基础:进程和线程之由来
- 【JavaScript】基础知识整理(一)
- 3.4 Zend_Db_Table_Row
- GDAL学习总结
- 快速排序(JAVA描述)
- View 生命周期
- 几个linux命令记录