- 论坛徽章:
- 17
|
本帖最后由 myworkstation 于 2013-07-07 16:04 编辑
因为加上sleep线程就成为 Cancel-Safe的。而使用Asynchronous cancellation模式时Cancel-Safe的才是Asynchronous-Cancel-Safety的。当取消的类型为PTHREAD_CANCEL_ASYNCHRONOUS时,新到的或未决的取消请求可能在任意时刻起作用.只有从一个线程中调用cancel-safe的函数时这个线程才是异步可取消的。不然不能保证cleanup handler被调用。C++的构造,析构函数都不是Cancel-Safe的所以也不能保证被执行(这个取决于实现LLVM和GCC的行为不同)。大部分库函数都不是async-cancel-safe的。linux相关文档没有明确说明哪些函数是async-cancel-safe的。posix标准只规定了三个async-cancel-safe的函数:pthread_cancel(), pthread_setcancelstate(), and pthread_setcanceltype()。
man
When cancelability is enabled and the cancelability type is PTHREAD_CANCEL_ASYNCHRONOUS, new or pending cancellation requests may be acted upon at any time。only functions that are cancel-safe may be called from a thread that is asynchronously cancelable.
POSIX :
Async-Cancel Safety
A function is said to be async-cancel-safe if it is written in such a way that entering the function with asynchronous cancelability enabled will not cause any invariants to be violated, even if a cancellation request is delivered at any arbitrary instruction. Functions that are async-cancel-safe are often written in such a way that they need to acquire no resources for their operation and the visible variables that they may write are strictly limited.
Any routine that gets a resource as a side-effect cannot be made async-cancel-safe (for example, malloc()). If such a routine were called with asynchronous cancelability enabled, it might acquire the resource successfully, but as it was returning to the client, it could act on a cancellation request. In such a case, the application would have no way of knowing whether the resource was acquired or not.
Indeed, because many interesting routines cannot be made async-cancel-safe, most library routines in general are not async-cancel-safe. Every library routine should specify whether or not it is async-cancel safe so that programmers know which routines can be called from code that is asynchronously cancelable |
|