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 ?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
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)
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 belowBut like most of the time, the best way to know for sure would be to try it with a test code.
I'm not familiar with
cond_wait
, but I presume it's from another library than the typically usedpthread_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 callingpthread_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.