CPP WINDOWS:有微秒级睡眠功能吗?

发布于 2025-01-02 15:40:55 字数 39 浏览 2 评论 0原文

我知道有毫秒(睡眠(毫秒)),

但我找不到微秒。

I know there is for milliseconds (Sleep(milli))

but I couldn't find one for micro..

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

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

发布评论

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

评论(4

我的奇迹 2025-01-09 15:40:55

VS 11 开发预览版包括处理线程的标准库部分。所以现在你可以说:

std::this_thread::sleep_for(std::chrono::microseconds(1));

当然,这并不意味着线程将在恰好这个时间后唤醒,但它应该在平台(和库实现)允许的范围内尽可能接近。正如其他评论所指出的,Windows 实际上不允许线程休眠这么短的时间。

The VS 11 dev preview includes the part of the standard library dealing with threads. So now you can say:

std::this_thread::sleep_for(std::chrono::microseconds(1));

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.

无人问我粥可暖 2025-01-09 15:40:55

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.

陌生 2025-01-09 15:40:55

您可以使用 rdtsc 指令或 QueryPerformanceCounter 用于获取高分辨率计数器的 Windows API 函数。然后,您可以使用 GetTickCount 或时间函数等来校准它们。

You can use rdtsc instruction or QueryPerformanceCounter Windows API function to get high-resolution counters. You can calibrate them then with GetTickCount, or time functions for example.

别把无礼当个性 2025-01-09 15:40:55

我刚刚写了一篇关于 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?

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