Q:

How to wait for a linux kernel thread (kthread)to exit?

I have just started programing on Linux kernel threads. I have a problem which I would like to share with you guys. My code is:
void do_big_things(void *data)
{
   // do some really big things
} 
struct task_struct *t1;
struct task_struct *t2;

void calling_fucntion()
{
   for(j =0; j < 100; j++)
   {
      t1 = kthread_run(do_big_things, &data1, "thread1");
      t2 = kthread_run(do_big_things, &data2, "thread2");
   }
}

Now as far as I have concluded to this problem (I may be wrong) is that threads t1 and t2 are created and run by kernel and then program goes back at the start of loop to created and run another two threads. As there is no condition to wait for these threads to finish, the kernel creates so many threads hence causing a stack overflow.
All I want is to know that how to make program wait for these two threads to finish and then go back to loop starting another two threads.
Any help would be appreciated. 

A:

/* Wait for kthread_stop */
set_current_state(TASK_INTERRUPTIBLE);
while (!kthread_should_stop()) {
    schedule();
    set_current_state(TASK_INTERRUPTIBLE);
}

Check this article for more information: "Sleeping in the Kernel".

