When does one use pthread_cancel and not pthread_kill?
When does one use pthread_cancel and not pthread_kill ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would use neither of those two but that’s just personal preference.
Of the two,
pthread_cancelis the safest for terminating a thread since the thread is only supposed to be affected when it has set its cancelability state to true usingpthread_setcancelstate().In other words, it shouldn’t disappear while holding resources in a way that might cause deadlock. The
pthread_kill()call sends a signal to the specific thread, and this is a way to affect a thread asynchronously for reasons other than cancelling it.Most of my threads tends to be in loops doing work anyway and periodically checking flags to see if they should exit. That’s mostly because I was raised in a world when
pthread_kill()was dangerous andpthread_cancel()didn’t exist.I subscribe to the theory that each thread should totally control its own resources, including its execution lifetime. I’ve always found that to be the best way to avoid deadlock. To that end, I simply use mutexes for communication between threads (I’ve rarely found a need for true asynchronous communication) and a flag variable for termination.