pthread_cancel vs pthread_exit vs pthread_join

 

pthread_cacel 的 man 解释是:

 

pthread_cancel  -------- cancel execution of a thread

 

The pthread_cancel() function shall request that thread be canceled. The target thread's cancelability state and type determines  when   the  cancellation  takes effect. When the cancellation is acted on, the cancellation cleanup handlers for thread shall be called. When  the last cancellation cleanup handler returns, the thread-specific data destructor functions shall be called for thread. When the last  destructor function returns, thread shall be terminated.   The  cancellation  processing  in  the  target  thread  shall  run  asynchronously  with  respect to the calling thread returning from  pthread_cancel().

 

pthread_cancel是对一个线程执行取消。该函数请求对一个线程进行消除。目标线程的消除状态与类型决定了何时消除请求取得影响。当消除点被置位, 线程的消除清理句柄就会被调用。当线程的最后一个消除清理句柄返回, 该线程相关的数据析构函数将被调用。 当最后一个析构函数返回, 线程就被终止了。在目标线程中异步运行的消除过程视pthread_cancel返回的调用线程而定。

 

 

什么是cancellation呢?一个比较过的去的解释是这样的:

Thread cancellation allows a thread to terminate the execution of any application thread in the process. Cancellation is useful when further operations of one or more threads are undesirable or unnecessary.

引用网址http://compute.cnr.berkeley.edu/cgi-bin/man-cgi?cancellation

大概意思是线程的未来操作不再必要时候,cancellation可以发挥作用;”线程取消“ 允许线程终止运行中的应用线程的执行。





来看一下另外一个关于cancel


机制的不错解释:

The cancel mechanism requests termination of anotherthread (or itself).

When you request that a thread be canceled, you are requesting thatit terminate as soon as possible. However, the target thread cancontrol how quickly it terminates by controlling its cancelabilitystate and type.

A thread's initial cancelability state is enabled.Cancelability state determines whether a thread canreceive a cancellation request. If the cancelability state isdisabled, the thread does not receive any cancellation requests.The current thread's cancelability state can be changed by callingpthread_setcancelstate.

Initially, a thread's cancelability type is deferred.Deferred cancelability means that threads receive acancellation request only at cancellation points-for example,when a call to the pthread_cond_wait routine is made. If you seta thread's cancelability type to asynchronous, the thread canreceive a cancellation request at any time. The current thread'scancelability type can be changed by calling pthread_setcanceltype.

 

消除机制是请求对某个(可以包括自身)线程的终止。当你请求对一个线程消除时,你在请求尽快的终止该线程。但是,目标线程可以藉由控制消除状态和类型来控制在多块程度上该线程被终止。消除状态Cancelability state决定了一个线程是否可以获取消除请求,如果消除状态被置位为空,则这个线程就收不到任何的消除请求。当前线程的这种消除状态属性可以通过调用pthread_setcancelstate来设置。默认情况下,某个线程的消除类型是被延迟的(deferred, 这种翻译不够准确),所谓 “延迟消除”指 线程仅仅在消除点上可以收到消除请求,怎么来理解线程的这种行为?举例来说:当调用 pthread_cond_wait 例程被生成,如果你将线程的消除类型cancelability type 设置为异步,则线程在任何时候都可以收到消除请求。当前线程的消除类型可以通过pthread_setcanceltype进行设置。

 以下举例几个消除点cancellationpoints,一个重要的线程概念):

  • pthread_setcanceltype (当类型被设置为异步时)
  • pthread_join
  • pthread_cond_wait
  • pthread_cond_timedwait

If the cancelability state is enabled, you can request the deliveryof any pending cancel request by using the pthread_testcancel orroutine. This routine allows you to permit cancellation to occurat places where it might not otherwise be permitted, and it isespecially useful within very long loops to ensure that cancelrequests are noticed within a reasonable time.

如果消除状态被置位, 就可以对任何“被挂起的消除请求”请求即进行递交(通过pthread_testcancel或例程routine来实现)。 这种例程routine使得对编程获得了这样一种程序控制行为:允许消除发生在另外情况不许可的地点。 程序的这种行为在多次循环准中非常有用,可以保证消除请求cancelrequests在一个合理的时间内可以被注意到。

可能起一个疑问,好像这个是pthread_exit的用武之地, 接下来就看一下pthread_exit 的解释:







The pthread_exit() function shall terminate the calling thread and make the value value_ptr available to 



any successful join with  the terminating  thread.  







pthread_exit实现的功能是终止调用的线程,并且使其指针值能被任何成功的join的终止线程所调用。(join又是一个什么样的概念?)



从以上两个概念看,pthread_cancel是间接的使线程终止,而pthread_exit,则是直接使线程终止。











来看一下前面提到的join是什么概念?

pthread_join的 man 解释是:

The pthread_join() function shall suspend execution of the calling thread until the target thread terminates, unless the target thread  has  already  terminated.

 

join()是将调用线程悬起,即调用线程的状态变为S, 直至目标线程终止(这里又遇到了终止terminate)

是否可以认为与join相对应的是pthread_detach?这个函数与pthread_exit之间的关系又是怎么样的?

为什么要设计看似如此繁杂的一套系统?可以通过消除机制来作为理解线程机制的入口,上文作了简单的介绍。

介绍thrdad暂时告一段落,有机会将进一步对线程展开进一步学习!

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值