windows中c语言的睡眠函数。 是否存在精度更高的函数?

发布于 2024-07-23 12:13:19 字数 136 浏览 7 评论 0原文

我想知道除了 Sleep() 之外,是否有人知道可以在 c 中的 windows 中使用更好的睡眠函数,它需要毫秒输入,并且仅保证输入是经过的最短时间。 我在 1 毫秒内通过,但实际上延迟了 15-16 毫秒。 有没有办法可以准确的设定指定的睡眠时间呢?

I was wondering if anyone knew of a better sleep function that could be used in windows in c, other than Sleep(), which takes a millisecond input and only guarantees that the input is the minimum amount of time that elapses. I am passing in 1 millisecond, but actually getting a 15-16 millisecond delay. Is there any way to accurately set a specified sleep time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

复古式 2024-07-30 12:13:20

不,Windows 上没有更好的睡眠功能,因为毫秒是可用的粒度级别。 如果你想减少睡眠时间,你需要实现某种自旋锁。

No, there isn't a better sleep function on Windows because milliseconds is the level of granularity available. If you want to sleep for less time you'll need to implement a spin lock of sorts.

情话难免假 2024-07-30 12:13:19

不,不是真的。 当您告诉程序休眠时,您就放弃了处理器并让操作系统决定下一步要做什么。 一旦操作系统获得控制权,它就会决定何时给你的程序另一片处理时间。 这就是为什么您可以指定最小值,但不能保证操作系统会在任何特定时间返回您的进程。

No, not really. When you tell your program to sleep, you're giving up the processor and letting the operating system decide what to do next. Once the operating system is in control, it decides when to give your program another slice of processing time. This is why you can specify a minimum, but there's no guarantee that the OS will get back to your process at any particular time.

看海 2024-07-30 12:13:19

睡眠仅保证您的进程至少n毫秒内不会执行,而不是保证它完全n会睡眠毫秒。 也许您想设置一个计时器? 或忙等待(即在循环中轮询 QueryPerformanceCounter)。

我会尽量避免这种方法(忙等待),除非你必须这么做,但你会消耗周期并且阻止CPU进入较低的时钟速度(即消耗电池寿命)

Sleep only guarantees that your process will not execute for at least n milliseconds, not that it will sleep exactly n milliseconds. Maybe you want to set up a timer? Or busy wait (i.e. poll QueryPerformanceCounter in a loop).

I'd try to avoid this approach (busy wait) unless you have to though, you're burning cycles and you're stopping the CPU from being put into lower clock speeds (i.e. burning battery life)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文