问题
I have my main process send pthread_cancel
to another thread which is waiting for a condition to happen with cond_wait(&condition)
. On the pthread_cancel
they are saying : Deferred cancel ability means that cancellation will be delayed until the thread next calls a function that is a cancellation point. But often those function are blocking function. Then my question is the thread cancelled only after that thread has been unblock (in my example by a broadcast or a signal) or it would see that i am currently blocking on a cancellation point and then cancelled my thread ?
回答1:
I'm not familiar with cond_wait
, but I presume it's from another library than the typically used pthread_cond_wait
?
But yes, if a thread is blocked in a pthread_cond_wait
and then cancelled, the thread will be woken up, reacquire it's mutex, and then be canceled.
There are thus two important points here to keep in mind when canceling threads that are blocked on a condition:
Make sure that the mutex is unlocked (or will be unlocked at some point in the future), before calling
pthread_cancel
. For instance, if thread A is waiting on a condition, and thread B locks the condition mutex, callspthread_cancel
and thenpthread_join
before unlocking the condition mutex, you'll deadlock.Install a cleanup handler (see pthread_cleanup_push) to unlock your condition mutex before calling
pthread_cond_wait
- otherwise you'll cancel your thread and leave the mutex locked.
However, note also that the pthread condition variable implementation has had/has some bugs - so be sure to use an up-to-date glibc.
回答2:
You might want to use pthread_cond_wait instead of cond_wait.
If you use pthread_cond_wait and based on this from man pthread_cond_wait(3)
A condition wait (whether timed or not) is a cancellation point. When the cancelability enable state of a thread is set to PTHREAD_CANCEL_DEFERRED, a side effect of acting upon a cancellation request while in a condition wait is that the mutex is (in effect) re-acquired before calling the first cancellation cleanup handler. The effect is as if the thread were unblocked, allowed to execute up to the point of returning from the call to pthread_cond_timedwait() or pthread_cond_wait(), but at that point notices the cancellation request and instead of returning to the caller of pthread_cond_timedwait() or pthread_cond_wait(), starts the thread cancellation activities, which includes calling cancellation cleanup handlers.
It looks like the thread will cancel on pthread_cond_wait even if it's currently blocked
Or you could set the cancellation type with pthread_setcanceltype to ASYNCHRONOUS. see comment below
But like most of the time, the best way to know for sure would be to try it with a test code.
来源:https://stackoverflow.com/questions/15641145/thread-cancel-and-blocking-function-as-cond-wait