thread_cancel和阻塞功能为cond_wait [英] thread_cancel and blocking function as cond_wait

查看:208
本文介绍了thread_cancel和阻塞功能为cond_wait的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主进程将pthread_cancel发送到另一个线程,该线程正在等待cond_wait(&condition)发生的情况.在pthread_cancel上他们说:延迟的取消功能意味着取消将被延迟,直到线程接下来调用一个作为取消点的函数.但是,这些函数通常是阻塞函数.然后我的问题是,只有在该线程被解除阻塞后才取消线程(在我的示例中是通过广播或信号),否则它会看到我当前正在取消点上阻塞,然后取消了我的线程?

I have my main process send pthread_cancel to another thread which is waiting for a condition to happen with cond_wait(&condition). On the pthread_cancel they are saying : Deferred cancel ability means that cancellation will be delayed until the thread next calls a function that is a cancellation point. But often those function are blocking function. Then my question is the thread cancelled only after that thread has been unblock (in my example by a broadcast or a signal) or it would see that i am currently blocking on a cancellation point and then cancelled my thread ?

推荐答案

您可能希望使用pthread_cond_wait而不是cond_wait.

You might want to use pthread_cond_wait instead of cond_wait.

如果您使用pthread_cond_wait并基于man pthread_cond_wait(3)的相关内容

If you use pthread_cond_wait and based on this from man pthread_cond_wait(3)

条件等待(无论是否定时)是取消点.当线程的可取消启用状态设置为PTHREAD_CANCEL_DEFERRED时,在条件等待期间对取消请求执行操作的副作用是(实际上)重新获取了互斥锁,然后调用第一个取消清除处理程序.效果就好像该线程是未阻塞的,直到从调用返回到pthread_cond_timedwait()或pthread_cond_wait()的返回点为止都可以执行,但是此时通知取消请求,而不是返回到pthread_cond_timedwait()的调用者或pthread_cond_wait(),启动线程取消活动,其中包括调用取消清除处理程序.

A condition wait (whether timed or not) is a cancellation point. When the cancelability enable state of a thread is set to PTHREAD_CANCEL_DEFERRED, a side effect of acting upon a cancellation request while in a condition wait is that the mutex is (in effect) re-acquired before calling the first cancellation cleanup handler. The effect is as if the thread were unblocked, allowed to execute up to the point of returning from the call to pthread_cond_timedwait() or pthread_cond_wait(), but at that point notices the cancellation request and instead of returning to the caller of pthread_cond_timedwait() or pthread_cond_wait(), starts the thread cancellation activities, which includes calling cancellation cleanup handlers.

即使该线程当前已被阻止,该线程似乎也会在pthread_cond_wait上取消

It looks like the thread will cancel on pthread_cond_wait even if it's currently blocked

或者您可以将pthread_setcanceltype的取消类型设置为ASYNCHRONOUS.参见下面的评论

但是像大多数时候一样,最好的确定方法是尝试使用测试代码.

But like most of the time, the best way to know for sure would be to try it with a test code.

这篇关于thread_cancel和阻塞功能为cond_wait的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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