背景
高精度定時器使用示例文章來源地址http://www.zghlxwxcb.cn/news/detail-805583.html
代碼
#include <linux/hrtimer.h>
#include <linux/jiffies.h>
static struct hrtimer my_hrtimer;
enum hrtimer_restart my_hrtimer_callback(struct hrtimer *timer)
{
// 定時器到期時要執(zhí)行的操作
// ...
return HRTIMER_NORESTART; // 或者重新啟動定時器
}
static int __init my_hrtimer_init(void)
{
ktime_t ktime;
// 初始化 hrtimer
hrtimer_init(&my_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
// 設(shè)置定時器到期時間(例如,500ms)
ktime = ktime_set(0, 500 * 1000000L); // 500ms
// 設(shè)置回調(diào)函數(shù)
my_hrtimer.function = my_hrtimer_callback;
// 啟動 hrtimer
hrtimer_start(&my_hrtimer, ktime, HRTIMER_MODE_REL);
return 0;
}
static void __exit my_hrtimer_exit(void)
{
// 取消 hrtimer
hrtimer_cancel(&my_hrtimer);
}
module_init(my_hrtimer_init);
module_exit(my_hrtimer_exit);
文章來源:http://www.zghlxwxcb.cn/news/detail-805583.html
到了這里,關(guān)于Linux內(nèi)核 -高精度定時器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!