CPP WINDOWS:有微秒级睡眠功能吗?
我知道有毫秒(睡眠(毫秒)),
但我找不到微秒。
I know there is for milliseconds (Sleep(milli))
but I couldn't find one for micro..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我知道有毫秒(睡眠(毫秒)),
但我找不到微秒。
I know there is for milliseconds (Sleep(milli))
but I couldn't find one for micro..
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
VS 11 开发预览版包括处理线程的标准库部分。所以现在你可以说:
当然,这并不意味着线程将在恰好这个时间后唤醒,但它应该在平台(和库实现)允许的范围内尽可能接近。正如其他评论所指出的,Windows 实际上不允许线程休眠这么短的时间。
The VS 11 dev preview includes the part of the standard library dealing with threads. So now you can say:
Of course this doesn't mean the thread will wake up after exactly this amount of time, but it should be as close as the platform (and library implementation) allows for. As other comments have pointed out, Windows doesn't actually allow threads to sleep for durations this short.
Windows 的睡眠时间不能少于一毫秒。时间片往往远高于 1 毫秒,因此即使线程具有最高优先级,这实际上也是不可能的。
如果你不关心消耗CPU,你可以旋转直到QueryPerformanceCounter你的时间已经过去了。
Windows can't sleep for less than a millisecond. Time slices tend to be much higher than 1ms, so it isn't really possible even with a thread a highest priority.
If you don't care about burning CPU, you can spin until QueryPerformanceCounter has elapsed your time.
您可以使用
rdtsc
指令或QueryPerformanceCounter
用于获取高分辨率计数器的 Windows API 函数。然后,您可以使用 GetTickCount 或时间函数等来校准它们。You can use
rdtsc
instruction orQueryPerformanceCounter
Windows API function to get high-resolution counters. You can calibrate them then withGetTickCount
, or time functions for example.我刚刚写了一篇关于 sleep() 函数和旋转的详细评论
性能计数器。为了避免再次在此处输入,请参阅以下链接:
c++,usleep() 已过时,Windows/MingW 的解决方法?
I just wrote a detailed comment about the sleep() function and spinning the
performance counter. To avoid typing it here again, here is the link:
c++, usleep() is obsolete, workarounds for Windows/MingW?