title: How to join a thread in Linux kernel?
body: <p>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'm not sure how we can wait for a single thread in the main thread to be completed (suppose we need the thread[3] be done then proceed):</p>
  
  <pre><code>#include &lt;linux/kernel.h&gt;
  #include &lt;linux/string.h&gt;
  #include &lt;linux/errno.h&gt;
  #include &lt;linux/sched.h&gt;
  #include &lt;linux/kthread.h&gt;
  #include &lt;linux/slab.h&gt;
  
  void *func(void *arg) {
      // doing something
      return NULL;
  }
  
  int init_module(void) {
      struct task_struct* thread[5];
      int i;
      for(i=0; i&lt;5; i++) {
          thread[i] = kthread_run(func, (void*) arg, "Creating thread");
      }
      return 0;
  }
  
  void cleanup_module(void) {
      printk("cleaning up!\n");
  }
  </code></pre>