这让我有两个问题:
>我错了,还是glibc / NPTL行为真的这个危险的打破?如果是这样,这样的危险行为是否符合?
>根据POSIX,如果在进程正在执行一个取消点的函数时调用一个信号处理程序,那应该是什么?
编辑:我几乎相信,任何线程是pthread_cancel的潜在目标,必须确保在线程上下文中不能从信号处理程序中调用取消点的函数:
一方面,可以在线程中调用的任何可能被取消的信号处理程序,并且使用任何异步取消不安全功能的任何信号处理程序必须在调用任何作为取消点的函数之前禁用取消.这是因为从信号中断的代码的角度来看,任何这样的取消将等同于异步取消.另一方面,信号处理程序无法禁止取消,除非调用信号处理程序时将运行的代码仅使用异步信号安全功能,因为pthread_setcancelstate不是异步信号安全的.
解决方法
至于POSIX是否允许这种行为,我还没有百分百的肯定. POSIX声明:
Whenever a thread has cancelability enabled and a cancellation request has been made with that thread as the target,and the thread then calls any function that is a cancellation point (such as pthread_testcancel() or read()),the cancellation request shall be acted upon before the function returns. If a thread has cancelability enabled and a cancellation request is made with the thread as a target while the thread is suspended at a cancellation point,the thread shall be awakened and the cancellation request shall be acted upon. It is unspecified whether the cancellation request is acted upon or whether the cancellation request remains pending and the thread resumes normal execution if:
The thread is suspended at a cancellation point and the event for which it is waiting occurs
A specified timeout expired
before the cancellation request is acted upon.
推测执行信号处理程序不是“暂停”的情况,所以我倾向于将glibc的行为解释为不符合要求.