【发布时间】:2021-04-06 20:00:30
【问题描述】:
我需要一个 60HZ 定时器(16.6 毫秒触发一次)
它在 windows(mingw gcc) 中运行良好,但在 liunx(gcc) 中运行良好
谁能帮我解决这个问题?谢了
#include <stdio.h>
#include <time.h>t
#define PRE_MS CLOCKS_PER_SEC / 1000
int main()
{
clock_t pre = clock();
int cnt = 0;
printf("CLOCKS_PER_SEC = %d\n", CLOCKS_PER_SEC);
while (1)
{
clock_t diff = clock() - pre;
if (diff > 16 * PRE_MS)
{
cnt++;
if (cnt > 60)
{
printf("%d\n", (int)pre);
cnt = 0;
}
pre += diff;
}
}
}
- windows 中的 printf pre 1s
CLOCKS_PER_SEC = 1000
1020
2058
3095
4132
5169
6206
7243
8280
9317
- linux 中的 printf pre 2s
CLOCKS_PER_SEC = 1000000
1875000
3781250
5687500
7593750
9500000
11406250
13312500
15218750
【问题讨论】:
-
请不要在发布答案后更改代码。
-
仍然是这个问题,我只是想让我的代码更干净,你的代码似乎对我有用,我稍后会检查它,thx