There is discussion of pthread cancellation points (http://man7.org/linux/man-pages/man3/pthread_cancel.3.html) on several question. In some cases the respondents say that cancellation points should not be used unless the programmer knows what they are doing very well.
My question---what are pthread cancellation points used for?
[updated from comment]
Does a cancellation point allow those specific API calls to be canceled?
Sticking with PTHREAD_CANCEL_DEFERRED, this may be the the center of the confusion. You are almost thinking of it backwards. A cancellation point doesn't allow system calls (API in your nomenclature) to be canceled, the calls check whether a cancellation request is pending. Conceptually you can think of this as (manually) setting a switch. The automatic cancellation points will check if it is in effect and a deliberate call to pthread_testcancel
can be inserted where convenient or otherwise necessary - for instance, in the middle of a long running calculation loop where there might not otherwise be cancellable system calls. The cancellation points automatically force calls to any pthread cleanup handlers that were set up and which can be chained in a manner similar to atexit
calls.
A lot of people choose (if it is workable for their situation) to set their own switch and code a means of shutting down the thread at that point in the thread when they check the switch. PTHREAD_CANCEL_DEFERRED is basically that on steroids. It provides (potentially) several points where that switch is conceptually check and thereby also forces the developer to consider the implications of being canceled at every one of those points.
Why those and not others?
You would need to discuss this on a call by call basis but primarily because of unpredictable side effects.
Would you want to use cancellation points in user-level code or just in APIs?
Yes, you want to use them in user-level code. You as the programmer are in the best position to know what resources you are using and how to clean them up if the thread is canceled and the logical implications of cancelization at any given point.
PTHREAD_CANCEL_ASYNCHRONOUS is another ball of wax and nightmare of its own. Conceptually you can almost think of this as the thread getting blasted by a kill
signal - it can be interrupted anywhere - but it does provide the opportunity for the cleanup handlers to run. The problem is that it is really hard to do that e.g. what if it is canceled in the middle of a malloc
? This makes it pretty much useless outside of some really, really, carefully considered situations.