为while循环创建超时的最干净方法是什么? [英] What is the cleanest way to create a timeout for a while loop?

查看:403
本文介绍了为while循环创建超时的最干净方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows API / C / C ++

Windows API/C/C++

1. ....  
2. ....
3. ....    
4.    while (flag1 != flag2)
5.    {
6.      SleepEx(100,FALSE);   
        //waiting for flags to be equal (flags are set from another thread).
7.    }
8. .....
9. .....  

如果这些标志在7秒后彼此不相等,我想继续第8行。

If the flags don't equal each other after 7 seconds, I would like to continue to line 8.

我们将不胜感激。谢谢。

Any help is appreciated. Thanks.

推荐答案

如果您正在等待设置特定标志或等待某个时间,可能会采用一种更为简洁的解决方案使用自动/手动重置事件。这些是为在线程之间发出信号而设计的,并在它们之上设计了非常丰富的API。例如,您可以使用WaitForMultipleObjects API,该API需要一个明确的超时值。

If you are waiting for a particular flag to be set or a time to be reached, a much cleaner solution may be to use an auto / manual reset event. These are designed for signalling conditions between threads and have very rich APIs designed on top of them. For instance you could use the WaitForMultipleObjects API which takes an explicit timeout value.

这篇关于为while循环创建超时的最干净方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆