为while循环创建超时的最干净方法是什么?

编程入门 行业动态 更新时间:2024-10-08 13:31:00
本文介绍了为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循环创建超时的最干净方法是什么?

本文发布于:2023-10-19 17:38:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508277.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:干净   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!