我知道POSIX sleep(x)函数使程序在x秒内休眠。有没有一个函数使程序在C ++中休眠x 毫秒?
在C ++ 11中,你可以使用标准库工具来完成此操作:
#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));
清晰可读,不需要猜测sleep()函数需要什么单位。
请注意,毫秒级没有标准的C API,因此(在Unix上),您将必须解决usleep,它接受微秒:
#include <unistd.h>
unsigned int microseconds;
...
usleep(microseconds);