reentrant,thread-safe 和 async-signal-safe
来源:互联网 发布:仙界网络直播间txt书包 编辑:程序博客网 时间:2024/05/22 16:34
reentrant,thread-safe 和 async-signal-safe
January 21st, 2009 by 王 聪Leave a reply »
先上定义吧,POSIX对它们的定义分别是:
Reentrant Function
A function whose effect, when called by two or more threads, is guaranteed to be as if the threads each executed the function one after another in an undefined order, even if the actual execution is interleaved.
Thread-Safe
A function that may be safely invoked concurrently by multiple threads. Each function defined in the System Interfaces volume of IEEE Std 1003.1-2001 is thread-safe unless explicitly stated otherwise.
Async-Signal-Safe Function
A function that may be invoked, without restriction, from signal-catching functions.No function is async-signal-safe unless explicitly described as such.
可重入我们都清楚,顾名思义,就是可以重新进入,进一步讲就是,用相同的输入,每次调用函数一定会返回相同的结果。这就是可重入。wikipedia上有更严谨的定义:
* Must hold no static (global) non-constant data.
* Must not return the address to static (global) non-constant data.
* Must work only on the data provided to it by the caller.
* Must not rely on locks to singleton resources.
* Must not call non-reentrant computer programs or routines.
然后是线程安全,从定义上看,它仅要求了可以安全地被线程并发执行。这是一个相对较低的要求,因为它内部可以访问全局变量或静态变量,不过需要加锁,也就是说,只要是在线程可控之中的,每次调用它返回不同的结果也没关系。到这里我们可以看出:可重入函数一定是线程安全的,而反之未必。wikipedia上也写道:
Therefore, reentrancy is a more fundamental property than thread-safety and by definition, leads to thread-safety: Every reentrant function is thread-safe, however, not every thread-safe function is reentrant.
例子,有很多,最出名的莫过于strtok(3),我们认识可重入这个概念就是从它开始的,它内部适用了静态变量,显然是不可重入的(它的可重入版是strtok_r(3))。其次应该是malloc(3),嘿嘿,其实也很明显,我就不多说了。但是,strtok(3)不是线程安全的,而malloc(3)是。
还有一个概念我们不常碰到,那就是异步信号安全,它其实也很简单,就是一个函数可以在信号处理函数中被安全地调用。看起来它似乎和线程安全类似,其实不然,我们知道信号是异步产生的,但是,信号处理函数是打断主函数(相对于信号处理函数)后执行,执行完后又返回主函数中去的。也就是说,它不是并发的!
一个函数,它访问了全局变量,那么它就是不可重入的,不过我们可以把它变成线程安全的,加上锁就可以,但是这种方法并不会把它变成异步信号安全的,而几乎可以肯定的是,使用了锁的一定不是信号安全的(除非屏蔽了信号,显然),信号完全可以在锁加上后解开前到来,然后就很可能形成死锁……这里有个很好的例子。所以,可重入的函数也一定是异步信号安全的,而反之未必。可以参考IBM上一篇不错的文章。
关于异步信号安全的函数列表可以参考man 7 signal;关于非线程安全的函数列表可以参考APUE第12.5节 ;关于可重入函数列表,可参考APUE第10.6节。另请参阅。
- reentrant,thread-safe 和 async-signal-safe
- reentrant,thread-safe 和 async-signal-safe
- reentrant,thread-safe 和 async-signal-safe
- thread-safe 和 reentrant
- 仔细探讨一下Thread-safe和Async-signal safe
- thread safe vs reentrant
- Qt reentrant、thread-safe
- thread safe vs reentrant
- Reentrant vs Thread-safe
- Thread-safe, Signal-safe
- ThreadSafe和Async-Signal-Safe的区别
- Race Conditions and Reentrant and thread-safe
- reentrant函数与thread safe函数浅析
- C Thread Safe and Reentrant Function Examples
- 可重入性 线程安全 Async-Signal-Safe
- Writing Reentrant and Thread-Safe Code(编写可重入和线程安全的代码)
- 编写可重入和线程安全的代码(Writing Reentrant and Thread-Safe Code)
- thread safe
- 代码中标注的方法
- HtmlEditor (在线html编辑器)的使用
- 数据库获取字段长度的函数
- 数据库 - in 和exists
- oracle与sql server2000中不在同一个服务器上的数据库数据操作的方法对比
- reentrant,thread-safe 和 async-signal-safe
- linux内核调试(kgdb)
- USACO Section 1.4 Packing Rectangles (POJ 1169)搜索
- android 显示16色的图片:输入用颜色矩阵,显示对应的16色位图
- poj 3278 Catch That Cow BFS
- Log4j的用法总结
- java 知识搜集
- Html Input标签只能输入数字
- PHP解析JSON与XML(简介)