I am trying to get my head around what exactly a cancellation point is in c++. I have read:
man page and What are pthread cancellation points used for
But I am still a little confused on certain points. For example, I am using the file write() function. Apparently this is a cancellation point. So when I call write(), I can see that another thread may start processing (so my code switches from the writing thread to another thread), this usually happens when the write-to buffer is full and needs to be emptied before the write() can succeed/complete.
But in my mind, this is not a cancellation of a thread, but merely a temporary blocking/suspend, and there is no thread "cleanup" to do...
So my question is, do cancellation points have relation to "blocking points"? - are they really the same thing, or is there some difference? Any clear "top-level" description of what a cancellation point is would be really useful.
The pthread_cancel() function requests cancellation of the target thread. The target thread is cancelled, based on its ability to be cancelled. When cancelability is disabled, all cancels are held pending in the target thread until the thread changes the cancelability.
Deferred cancellation − The target thread checks periodically whether it should terminate, allowing it an opportunity to terminate itself in an orderly fashion.
RETURN VALUEIf successful, the pthread_cancel() function returns zero. Otherwise, an error number is returned to indicate the error.
The default cancellation type is PTHREAD_CANCEL_DEFERRED. If cancellation is enabled and asynchronous cancellation has been requested, the thread must call only async-cancel-safe functions, since the thread may be cancelled by the system at any time.
When your thread gets pulled from execution, its state is saved by the OS and that is not a cancellation of the thread. The cancellation means thread termination, on request, with the specific intent of letting everything in a final state when completed (aka. all resources are freed, all handlers are updated, etc.).
What you call blocking can happen to a thread while in mid-cancellation.
Example: The thread gets a cancellation request. The OS queues it until the thread becomes cancellable. When the thread becomes cancellable, and the thread is executing a cancel point, the thread can be cleaned and cancelled. The write function is a cancellation point, this meaning it is safe from the point of view of the OS to cancel the thread while this function is executed (the state of all related resources will be consistent).
While the cancellation procedure is running, the thread can be blocked as many times as the OS sees fit.
As an additional note, if you look at the POSIX requirement for cancellation points, virtually all blocking interfaces are required to be cancellation points. Otherwise, on any completely blocked thread (in such call), there would be no safe way to terminate that thread.
http://man7.org/linux/man-pages/man7/pthreads.7.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With