Search code examples
cpthreadsposix

Is it safe to call pthread_cancel() on terminated thread?


I'm wondering if it is safe to call pthread_cancel() on a terminated thread. I couldn't find any hints in the manual page. Thanks in advance for any hints.

Edit: Maybe I wasn't accurate enough. I'm not talking about threads terminated by an earlier pthread_cancel() but about threads that simply returned from their thread function.


Solution

  • I think it needs to be safe, or pthread_cancel would be problematic (next to unusable).

    Indeed, if it wouldn't be safe, every call to pthread_cancel would have to be enormously complicated by checking the thread is alive (and ensuring it stays alive until you get to cancel it). A simple "are you still there" wouldn't do.

    In conclusion, I believe pthread_cancel must be safe if the thread has terminated. Of course, this might not be the case for a terminated and joined thread.