如何使用Linux工作队列 [英] How to use Linux Work Queue
问题描述
Linux工作队列是具有进程上下文的内核级线程.我试图将其用作没有特定进程上下文的kthread的替代方案.但是,如何将数据传递到工作队列? work_struct具有类型为atomic_long_t的数据字段.我无法将指针传递给该字段.我该怎么办?
Linux work queues are meant to be kernel level threads with process context. I was trying to use it as an alternative to kthread which has no specific process context. But how do I pass data to work queue? work_struct has a data field which is of type atomic_long_t. I could not pass pointer to this field. How do I do it?
我也找不到工作队列的一个具体示例.你能推荐一个吗?
Also I could not find a single concrete example of work queue. Can you suggest one?
推荐答案
如果要将数据传递到工作队列函数,只需将work_struct
结构嵌入到自己的数据结构中,并在工作函数中使用container_of
进行检索.
If you want to pass data to your work queue function, just embed the work_struct
structure inside your own data structure and use container_of
inside your work function to retrieve it.
作为一个简单的示例,内核充满了它-仅git grep work_struct
.您可以查看drivers/cpufreq/cpufreq.c
(handle_update
函数)作为一个简单示例.下面的文章在最后还嵌入了一个示例,但是它没有使用container_of
,而是依赖于一个事实,即结构的第一个成员与其父对象具有相同的地址:
As for a simple example, the kernel is full of it - just git grep work_struct
. You can look at drivers/cpufreq/cpufreq.c
(handle_update
function) for a simple example. The article below also embeds an example at the end, but it does not use container_of
and instead relies on the fact that the first member of a structure has the same address as its parent:
http://www.ibm.com/developerworks/linux/library/l -tasklets/index.html
这篇关于如何使用Linux工作队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!