在C ++跨平台中延迟几毫秒 [英] Delaying for milliseconds in C++ cross-platform
问题描述
我正在用C ++写一个多平台内部库,该库最终将在Windows,Linux,MacOS和ARM平台上运行,并且需要一次睡眠毫秒的方法。
I'm writing a multi-platform internal library in C++ that will eventually run on Windows, Linux, MacOS, and an ARM platform, and need a way to sleep for milliseconds at a time.
我有一个在ARM平台上执行此操作的准确方法,但是我不确定如何在其他平台上执行此操作。
I have an accurate method for doing this on the ARM platform, but I'm not sure how to do this on the other platforms.
在大多数平台上是否都可以以毫秒级的分辨率入睡,或者每个平台都需要特殊情况吗?
Is there a way to sleep with millisecond resolution on most platforms or do I have to special-case things for each platform?
推荐答案
对于Linux和Mac OS X,您可以使用 usleep
:
For Linux and Mac OS X you can use usleep
:
usleep(350 * 1000);
对于Windows,您可以使用 Sleep
:
For Windows you can use Sleep
:
Sleep(350);
编辑: usleep()
睡眠数微秒,而不是毫秒,因此需要进行调整。
usleep()
sleeps for microseconds, not milliseconds, so needs adjusting.
这篇关于在C ++跨平台中延迟几毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!