create_singlethread_workqueue & queue_work

这篇博客介绍了Linux系统中如何使用`create_singlethread_workqueue()`创建一个单线程工作队列,用于调度延迟执行的任务。工作队列的工作原理是将任务(函数指针及参数)提交到队列,由一个单独的 worker 线程执行。`INIT_WORK`宏用于运行时初始化工作结构,`queue_work()`函数则将工作提交到队列,等待执行。这提供了一种异步处理任务的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The function create_singlethread_workqueue() will create a work queue. A work queue is just a queue to which you submit work. "work" here means any function that you would want to run at a given point in time. This work will be executed by a single worker thread which is also created when you call create_singlethread_workqueue().

struct workqueue_struct *create_singlethread_workqueue(const char *name);

Secondly, INIT_WORK is a mechanism to initialize the work struct at run time. There is a way to do the initialization at compile time too.

INIT_WORK(struct work_struct *work, void (*function)(void *), void *data);

This means whenever we queue the work_struct *work on to a workqueue, the function pointed to by (*function) will be called with argument void *data. Let's see how we queue work:

int queue_work(struct workqueue_struct *queue, struct work_struct *work);

So here we have queued/submitted the work to a workqueue *queue. This will ultimately execute function pointed to by (*function) with argument void *data specified in the INIT_WORK()

 

Note: OLD API.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值