取消点到底是什么? [英] What exactly is a cancellation point?

查看:180
本文介绍了取消点到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚C ++中取消点到底是什么。我已阅读:

I am trying to get my head around what exactly a cancellation point is in c++. I have read:

手册页什么是pthread取消点?

但是我在某些方面仍然有些困惑。例如,我正在使用文件write()函数。显然,这是一个取消点。因此,当我调用write()时,我可以看到另一个线程可能开始处理(因此我的代码从写线程切换到另一个线程),这通常在写缓冲区已满且需要在写之前清空时发生()可以成功/完成。

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.

推荐答案

线程被从执行中拉出,其状态由操作系统保存,而不是取消线程。取消意味着根据请求终止线程,其具体目的是在完成时让所有内容都处于最终状态(也就是释放所有资源,更新所有处理程序等)。

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.

示例:线程获得取消请求。操作系统将其排队直到线程变为可取消状态。当线程变为可取消状态,并且线程正在执行取消点时,可以清除和取消线程。 write 函数是一个取消点,这意味着从OS的角度来看,执行该函数时取消线程是安全的(所有相关资源的状态将保持一致)。

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.

作为附加说明,如果您查看POSIX的取消点要求,实际上所有阻塞接口都必须是取消点。否则,在任何完全阻塞的线程上(在此类调用中),将没有安全的方法来终止该线程。

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

这篇关于取消点到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆