在我的MacOS (OS 10.11.2)中记录时间间隔(至少以毫秒计)有问题。
显然,其基本思想是将时间记录两次,然后进行减法。当我发现"sys/time.h“中有gettimeofday()和这个函数的结构- struct时,问题就发生了。然后我得到了时间间隔,并想让它按时间间隔睡觉。但是,无论是nanosleep()还是usleep()似乎都没有时间类型的参数(忽略睡眠(),因为我至少需要毫秒)。
我应该把时间转换成适合纳米睡眠()或usleep(),还是有一个更好更简单的方法?
发布于 2016-01-13 00:09:49
此示例使用select对timeval值进行睡眠(此处为10秒):
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/select.h>
int main(void)
{
printf("start...\n");
struct timeval tv = {10, 0};
select(0, NULL, NULL, NULL, &tv);
printf("done!\n");
return 0;
}https://stackoverflow.com/questions/34755730
复制相似问题