How to join a thread in Linux kernel?

前端 未结 2 1347
长情又很酷
长情又很酷 2021-01-06 19:07

The main question is: How we can wait for a thread in Linux kernel to complete? I have seen a few post concerned about proper way of handling threads in Linux kernel but i\'

2条回答
  •  [愿得一人]
    2021-01-06 19:39

    kthread_stop() is a kernel's way for wait thread to end.

    Aside from waiting, kthread_stop() also sets should_stop flag for waited thread and wake up it, if needed. It is usefull for threads which repeat some actions infinitely.

    As for single-shot tasks, it is usually simpler to use works for them, instead of kthreads.

    EDIT: Note: kthread_stop() can be called only when kthread(task_struct) structure is not freed.

    Either thread function should return only after it found kthread_should_stop() return true, or get_task_struct() should be called before start thread (and put_task_struct() should be called after kthread_stop()).

提交回复
热议问题