What exactly is a cancellation point?

混江龙づ霸主 提交于 2019-12-30 01:54:06

问题


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.


回答1:


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 seems fit.

As an additional note, if you look at the POSIX requirement for cancel points, virtually all blocking interfaces are required to be cancel 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




回答2:


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:

  • http://man7.org/linux/man-pages/man7/pthreads.7.html

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:

  • http://man7.org/linux/man-pages/man7/pthreads.7.html
  • http://man7.org/linux/man-pages/man3/pthread_cancel.3.html
  • http://man7.org/linux/man-pages/man3/pthread_setcancelstate.3.html
  • http://man7.org/linux/man-pages/man3/pthread_testcancel.3.html


来源:https://stackoverflow.com/questions/27374707/what-exactly-is-a-cancellation-point

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!