作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
C语言中定时器的使用方法是什么?我需要等到 500 毫秒才能找到工作。请提及完成这项工作的任何好方法。我使用了 sleep(3);
但是这个方法在这段时间内没有做任何工作。我有一些东西会尝试直到那个时候得到任何输入。
最佳答案
这是我使用的解决方案(它需要 #include <time.h>
):
int msec = 0, trigger = 10; /* 10ms */
clock_t before = clock();
do {
/*
* Do something to busy the CPU just here while you drink a coffee
* Be sure this code will not take more than `trigger` ms
*/
clock_t difference = clock() - before;
msec = difference * 1000 / CLOCKS_PER_SEC;
iterations++;
} while ( msec < trigger );
printf("Time taken %d seconds %d milliseconds (%d iterations)\n",
msec/1000, msec%1000, iterations);
关于c - 如何在 C 中使用定时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17167949/
我是一名优秀的程序员,十分优秀!