什么是用于pthread的取消点? [英] What are pthread cancelation points used for?

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

问题描述

有是(<的pthread的取消点讨论href=\"http://man7.org/linux/man-pages/man3/pthread_cancel.3.html\">http://man7.org/linux/man-pages/man3/pthread_cancel.3.html)几个问题,在某些情况下,受访者表示,除非程序员知道他们在做什么很好取消点不宜使用。

我的问题---是用来做什么的pthread取消点?

[从评论更新]


  1. 是否取消点让那些特定的API调用被取消?

  2. 为什么这些,而不是别人?

  3. 会有人想使用他们为别的?

  4. 他们是一个黑客以处理内核中的问题,或者是他们固有的必须符合POSIX的东西吗?

  5. 您想在用户级code或只是API的使用取消点?


解决方案

  

是否取消点让那些特定的API调用被取消?


与PTHREAD_CANCEL_DEFERRED坚持,这可能是混乱的中心。你几乎以为它倒退。取消点没有的允许的系统调用(在你的命名API)被取消,来电查询取消请求是否被挂起。从概念上你可以认为这是(手动)设置一个开关。自动取消点会检查它是否生效,可插入 pthread_testcancel 有意来电,其中便利或其他必要的 - 例如,在长时间运行计算的中间循环那里否则可能无法被撤销的系统调用。取消自动点强制分别设立并可以以类似于 atexit对调用的方式来链接任何pthread的清理处理程序调用。

很多人选择(如果它是可行的用于他们的情况)来设置自己的开关和code在螺纹点关闭线程的手段,当他们检查开关。 PTHREAD_CANCEL_DEFERRED基本上是类固醇。它提供了(潜在的)几个点,其中该交换机在概念上检查,从而也迫使开发商考虑在这些点中的每一个被取消的影响。


  

为什么这些,而不是别人?


您将需要讨论这个由呼叫的基础,但主要是因为未predictable副作用的电话。


  

您是否想在用户级code或只是API的使用取消点?


是的,要在用户级code使用它们。你作为程序员都处于最佳位置,知道你使用的是什么样的资源,以及如何清除它们如果线程被取消,cancelization在任何给定的点的逻辑含义。

PTHREAD_CANCEL_ASYNCHRONOUS是蜡和自己的噩梦另一球。从概念上讲,你几乎可以认为这是该线程得到由信号抨击 - 它可以在任何地方被打断 - 但它确实提供了清理处理程序运行的机会。问题是,这是真的很难做,例如如果它是在的malloc中间取消?这使得pretty多一些真的,真的,仔细考虑的情况外毫无用处。

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]

  1. Does a cancellation point allow those specific API calls to be canceled?
  2. Why those and not others?
  3. Would anyone want to use them for anything else?
  4. Are they a hack to deal with a problem in the kernel, or are they inherent in something having to with POSIX?
  5. Would you want to use cancellation points in user-level code or just in APIs?

解决方案

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.

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

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