1、定义数据结构
struct delayed_work work;
struct workqueue_struct *wq;
2、编写要被提交到工作队列中的函数,函数原形如下:
typedef void(*work_func_t)(struct work_struct *work);
3、创建一个专用的内核线程来执行提交到工作队列中的函数
wq = create_singlethread_workqueue(const char *name);
4、初始化数据结构
INIT_DELAYED_WORK(struct delayed_work *work, work_func_t func);
5、将任务提交到工作队列
queue_delayed_work(struct workqueue_struct *wq, struct delayed_work* work, unsigned long timeout);
6、删除提交到工作队列中的任务
int cancel_delayed_work(struct delayed_work *work);
7、刷新工作队列
void flush_workqueue(struct workqueue_struct *wq);
8、释放相关资源
void destory_workqueue(struct workqueue_struct *wq);
其中,第6、7、8项不是必须的,根据实际情景选择是否使用。
本文详细介绍了延时任务调度机制的实现步骤,包括定义数据结构、编写提交到工作队列中的函数、创建专用内核线程等。同时,还提供了刷新、取消任务及释放资源的方法。

1831

被折叠的 条评论
为什么被折叠?



