Search code examples
c++cpthreadsrtailxrt

Should I join a thread that has been killed?


I have many joinable threads that at some point need to all stop. The threads are pthread, but are created through RTAI's user-space interface (never mind the RTAI though).

For each thread I have a variable that I can set to tell them to quit. So what I do is:

  • set that variable for each thread so that they stop
  • Wait at most one second
  • join the threads

Now the thing is, since I am using RTAI, which uses a kernel-space buddy thread to the work for me, if something goes wrong, the thread may go unresponsive (It has never happened, but I have to be careful just in case something DOES go wrong). In such a case, I just kill the thread.

So, my question is, once you have a thread that is killed, should you join on it or not?


Solution

  • Killing a thread doesn't change whether you should join it or not. If the thread hasn't been detached, then you need to join it, or you will leak resources in the system.