2016-03-31 25 views
5

如果我有下面的内核线程功能:如果我们从内核线程返回,是否需要使用kthread_stop?

int thread_fn() { 
    printk(KERN_INFO "In thread1");  
    return 0; 
} 

我还需要在这里使用kthread_stop()功能?

请问return中的线程函数是否让内核线程停止并退出?

+0

我想你应该可以通过查看启动线程的代码来查看它是否能够设置线程的栈,并且返回的地址会在某个地方显示。 –

回答

1

如果你看kthread()implemented如何在第209行调用threadfn(data)并在ret中存储退出码;那么它叫do_exit(ret)

所以从您的threadfn简单返回就足够了。

如果你看看kthread_stop的文档,它说,它:

  • kthread_should_stop返回true;
  • 唤醒线程;
  • 等待线程退出。

这意味着kthread_stop()只应从线程外部调用来停止线程。由于它等待线程完成,所以不能在线程内调用它,否则可能会导致死锁!

此外,文档说它只通知线程它应该退出,线程应该调用kthread_should_stop来了解这个。因此,一个长寿命threadfn可以这样做:

int thread_fn() { 
    printk(KERN_INFO "In thread1"); 
    while (!kthread_should_stop()) { 
     get_some_work_to_do_or_block(); 
     if (have_work_to_do()) 
      do_work(); 
    } 
    return 0; 
} 

但是,如果你的函数不长寿命,呼吁kthread_should_stop是没有必要的。

相关问题