码迷,mamicode.com
首页 > 其他好文 > 详细

reentrant,thread-safe 和 async-signal-safe

时间:2019-12-11 21:04:41      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:other   可重入函数   sync   nes   const   执行   fun   严格   led   

可重入,线程安全和异步信号安全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.

通俗地讲,用相同的输入,每次调用函数一定会返回相同的结果。这就是可重入

严格地讲

* 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.

线程安全仅要求了可以安全地被线程并发执行

因为它内部可以访问全局变量或静态变量,不过需要加锁,也就是说,只要是在线程可控之中的,每次调用它返回不同的结果也没关系。到这里我们可以看出:可重入函数一定是线程安全的,而反之未必。

异步信号安全,它其实也很简单,就是一个函数可以在信号处理函数中被安全地调用。看起来它似乎和线程安全类似,其实不然,我们知道信号是异步产生的,但是,信号处理函数是打断主函数(相对于信号处理函数)后执行,执行完后又返回主函数中去的。也就是说,它不是并发的!

reentrant,thread-safe 和 async-signal-safe

标签:other   可重入函数   sync   nes   const   执行   fun   严格   led   

原文地址:https://www.cnblogs.com/guxuanqing/p/12024961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!