kthread kthread_stop kthread_should_stop 例子

启动内核线程
由kthread_create()和wake_up_process()两部分组成,这样的好处是kthread_run()创建的线程可以直接运行。

create and wake a thread.

1
2
3
4
5
6
7
8

({
struct task_struct *__k
= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__);
if (!IS_ERR(__k))
wake_up_process(__k);
__k;
})

kthread_stop

结束指定线程,设置should_stop标志

1
int (struct task_struct *k);

kthread_should_stop

返回should_stop标志,用于检查

1
int kthread_should_stop(void);

例子

  1. 创建

    1
    2
    data->auto_update = kthread_run(adt7470_update_thread, client,
    dev_name(data->hwmon_dev));
  2. threadfn

    1
    2
    3
    4
    5
    6
    static int adt7470_update_thread(void *p)
    {
    while (!kthread_should_stop()) {
    ...
    }
    }
  3. stop

    1
    kthread_stop(data->auto_update);