29

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.

Community
  • 1
  • 1
code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • 5
    A cancellation point in general is any point in the control flow where control returns to the scheduler. The only possible meaning of "cancellation" is to not get scheduled again, so you can only cancel something if you can influence the scheduling decisions. System calls form a natural interaction with the scheduler, though there may be others. – Kerrek SB Dec 09 '14 at 09:00
  • @KerrekSB With this comment and Bogdan V's answer, I think this is really making sense to me now, thanks :) – code_fodder Dec 09 '14 at 09:10
  • Thanks @KerrekSB for a clear, concise and solid explanation mentioning the scheduler. – daparic Aug 29 '16 at 21:23

2 Answers2

31

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

Tongfa
  • 2,078
  • 1
  • 16
  • 14
Bogdan V.
  • 719
  • 4
  • 7
  • 1
    Ahh, this makes more sense. So a cancellation point really is for cancelling threads, but there is a non-direct relation to "blocking points" because, as you say, otherwise you may get "deadlocked" threads that you can't even kill off... I think this answers my question and clears up my confusion :) – code_fodder Dec 09 '14 at 09:09
  • 3
    The deadlocked thread technically could be killed with pthread_kill. The target thread will get a SIGKILL. But for example, the destructors of objects in the thread local storage will not be called. Also, when killed, there is no guarantee the internal structures used for the thread will be freed. pthread_cancel is intended to be "the cleanest" way to do it when you don't get to do a join. – Bogdan V. Dec 09 '14 at 09:22
  • 2
    @BogdanV.: If you hit any thread in a process with SIGKILL, the whole process dies, not just the thread. It has to work that way because the regular kill syscall already picks a target thread "at random" and pthread_kill just lets you choose which thread to signal. POSIX [explicitly documents this](http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_kill.html): "Note that pthread_kill() only causes the signal to be handled in the context of the given thread; the signal action (termination or stopping) affects the process as a whole." – Kevin Aug 31 '18 at 17:43
21

When you want to terminate or cancel a thread from another thread (e.g., from the main thread) using pthread_cancel() the following happens (c.f.):

The pthread_cancel() function sends a cancellation request to the thread thread.

The target thread will not terminate at once, but rather when it reaches a cancellation point (c.f.):

POSIX.1 specifies that certain functions must, and certain other functions may, be cancellation points. If a thread is cancelable, its cancelability type is deferred, and a cancellation request is pending for the thread, then the thread is canceled when it calls a function that is a cancellation point.

Whether or not these functions that are a cancellation point may also block the execution of the thread, is not relevant at this point. There is a list of these functions in the documentation:

Note that there are settings that can influence the behaviour and "cancellability" of a thread that I have left out here for simplicity. For further reading:

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
  • Thanks for all the links, I have read through most of these, I think the source of my confusion was because I thought a "blocking" point was the same as a cancellation point which the other answers cleared up for me. Still, +1 for all the info :) – code_fodder Dec 09 '14 at 09:13