-1
我需要一個定時器,所以你能告訴我如何製作一個定時器,它會在每一毫秒中斷我,然後我將有一個變量來計算毫秒數,然後每20毫秒我會調用主函數?如何在c語言的linux中創建計時器?
我需要一個定時器,所以你能告訴我如何製作一個定時器,它會在每一毫秒中斷我,然後我將有一個變量來計算毫秒數,然後每20毫秒我會調用主函數?如何在c語言的linux中創建計時器?
如果沒有別的事情可做,然後等待20毫秒,然後做一些事情,然後再等待,下面的方法會做:
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
while (1)
{
struct timeval tv = {0, 20000}; /* 0 seconds, 20000 micro secs = 20 milli secs */
if (-1 == select(0, NULL, NULL, NULL, &tv))
{
if (EINTR == errno)
{
continue; /* this means the process received a signal during waiting, just start over waiting. However this could lead to wait cycles >20ms and <40ms. */
}
perror("select()");
exit(1);
}
/* do something every 20 ms */
}
return 0;
}
也許你想與我們分享你有什麼媒體鏈接試過嗎? – hetepeperfan
似乎已經有許多相關的問題與答案。 – jxh
[1ms解決方案計時器在linux下推薦的方式]可能的重複](http://stackoverflow.com/questions/240058/1ms-resolution-timer-under-linux-recommended-way) –