WaitForSingleObject 的超时解析 [英] Timeout resolution of WaitForSingleObject

查看:28
本文介绍了WaitForSingleObject 的超时解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 WaitForSingleObject 函数等待无信号事件时,我发现在某些情况下,调用将在指定的超时期限内返回 WAIT_TIMEOUT.只需在超时设置为 1000 毫秒的情况下循环调用,我已经看到调用在低至 990 毫秒的时间段内返回(在 WinXP 上运行).我正在使用 QueryPerformanceCounter 来获得独立于系统时钟的时间测量,所以我认为时钟漂移可能不是一个答案.

When I wait on a non-signaled Event using the WaitForSingleObject function, I find that in some cases the call will return WAIT_TIMEOUT in less than the specified timeout period. Simply looping on the call with a timeout set to 1000ms, I've seen the call return in periods as low as 990ms (running on WinXP). I'm using QueryPerformanceCounter to get a system-clock independent time measurement, so I don't think clock drift is likely to be an answer.

这种行为对我来说没有任何实际问题,但我想更好地理解它.看起来它可能在大致上以计时器滴答的分辨率工作.Microsoft 是否会发布有关此函数精度的更多详细信息?我应该期待在 Vista 中获得更高的精度吗?

This behavior doesn't present any practical problems for me, but I'd like to understand it better. It looks like it may be working at roughly the resolution of a timer tick. Does Microsoft publish any further details on the precision of this function? Should I expect greater precision in Vista?

推荐答案

是的,WaitForSingleObject 使用计时器刻度分辨率,它不使用像 QueryPerformanceCounter 这样的高分辨率计时器.

Yes, WaitForSingleObject uses the timer tick resolution, it does not use a high-resolution timer like QueryPerformanceCounter.

http://msdn.microsoft.com/en-us/library/ms687069(VS.85).aspx,MSDN 上关于等待函数"的文章对此进行了扩展:

http://msdn.microsoft.com/en-us/library/ms687069(VS.85).aspx, the MSDN article on "Wait Functions" expands on this:

指定超时的准确度间隔取决于分辨率系统时钟.系统时钟以恒定速率滴答".如果超时间隔小于系统时钟的分辨率等待可能会在少于规定的时间长度.如果超时间隔大于 1勾选但少于两个,等待可以在一到两个刻度之间的任何地方,等等.

The accuracy of the specified time-out interval depends on the resolution of the system clock. The system clock "ticks" at a constant rate. If the time-out interval is less than the resolution of the system clock, the wait may time out in less than the specified length of time. If the time-out interval is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on.

本文还解释了如何使用 timeBeginPeriod 来提高系统时钟分辨率 - 但不推荐这样做.

This article also explains how to use timeBeginPeriod to increase system clock resolution - but this is not recommended.

我能想到几个原因.首先,WaitForSingleObject 的几乎所有用例都不需要更高的分辨率.使用高分辨率计时器将需要内核不断轮询计时器(不可行,因为内核代码不能保证始终运行)或频繁重新编程以生成中断(因为可能有多个 WaitForSingleObjects 并且很可能只有一个可编程中断).

I can think of several reasons why. First, a higher resolution is not needed for nearly all use cases of WaitForSingleObject. Using a high-resolution timer would require the kernel to constantly poll the timer (not feasible since kernel code is not guaranteed to be always running) or reprogram it frequently to generate an interrupt (since there could be multiple WaitForSingleObjects and most likely only a single programmable interrupt).

另一方面,已经有一个时间源可以不断更新,其分辨率对于 WaitForSingleObject、SetWaitableTimer 和 Sleep 来说已经足够了.

On the other hand, there already is a timing source that is constantly updatable at a resolution that is more than good enough for WaitForSingleObject, SetWaitableTimer, and Sleep.

这篇关于WaitForSingleObject 的超时解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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