折返(Reentrancy)VS线程安全(Thread safety)
在Wiki上,折返例如,下面的定义(接)
In computing, a computer program or subroutine is called reentrant if it can be interrupted in the middle of its execution and then safely called again ("re-entered") before its previous invocations
complete execution. The interruption could be caused by an internal action such as a jump or call, or by an external action such as a hardware interrupt or signal. Once the reentered invocation completes, the previous invocations will resume correct execution.
翻译过来,就是说在函数运行的过程中。假设发生了中断被迫运行其他动作。其中断运行完毕后(中断运行过程中可能会再次调用该函数)。再次又一次返回函数的时候。可以恢复正确的运行过程。
显然。定义里面并没有强调必须是多线程,并且wiki上也进一步说明了一个事实
This definition originates from single-threaded programming environments where the flow of control could be interrupted by a hardware interrupt and transferred to an interrupt service routine
(ISR).
简单翻译。可重入的定义是依据单线程,而且控制流能够被硬件中断打断的环境的。因此普通情况下并没有涉及和考虑到多线程的情况。
我们再来看看线程安全的wiki定义(具体链接)
Thread safety is a computer programming concept applicable in the context of multi-threaded programs. A piece of code is thread-safe if it only manipulates shared data structures in a manner
that guarantees safe execution by multiple threads at the same time. There are various strategies for making thread-safe data structures.
简单翻译,线程安全就是保证多线程同一时候运行函数时,可以以保证安全运行的情况下操作共享数据。
这里强调了运行的上下文必须为多线程环境。
从以上两个概念的定义我们能够看到。可重入并不一定是线程安全,线程安全也并不一定是可重入的。在这里,能够举两个样例来说明
首先,是一个可重入但不是线程安全的样例。
下面代码段里。把局部变量s保留了全局变量t的状态,可以在中断发生之后恢复原来运行状态。符合可重入的定义。但非常明显。因为无法确保全局数据的一致性(没加锁)。因此不是线程安全
int t; void swap(int *x, int *y)
{
int s; s = t; // save global variable
t = *x;
*x = *y; // hardware interrupt might invoke isr() here!
*y = t;
t = s; // restore global variable
} void isr()
{
int x = 1, y = 2;
swap(&x, &y);
}
然后是一个线程安全但不是可重入的样例。因为对静态变量counter的訪问之前都相互排斥锁进行同步,因此是线程安全的。但假设获取了相互排斥锁,没有释放锁的过程里。发生了中断,这时候假设在中断里再次调用函数,则会发生死锁。因此不是可重入的。
#include <pthread.h> int increment_counter ()
{
static int counter = 0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&mutex); // only allow one thread to increment at a time
++counter;
// store value before any other threads increment it further
int result = counter; pthread_mutex_unlock(&mutex); return result;
}
由以上两个样例里,更加清晰地说明了两者之间并不一定相互决定的关系。一般来说,实现可重入要注意在函数运行过程中。假设要保存运行进度的状态,要考虑把状态保存到局部变量(栈),TLS。不能保存在全局或者静态变量,另外还要注意不能调用其他不可重入的函数。
最后。经过在网络上的一些调查搜索,发现了部分人对可重入有第二种理解。
简单来说,就是可重入也包含多线程的情况,也就是说当单一线程在函数运行过程中。会保证当另外线程运行的正确性。
也就是可重入包含了对单线程的重入和多线程的重入,显然后者有更强的限定性,同时也保证线程安全。
版权声明:本文博客原创文章,博客,未经同意,不得转载。
折返(Reentrancy)VS线程安全(Thread safety)的更多相关文章
- 线程安全 Thread Safety Problem scala concurrency 并发
小结: 1.基于java并发模型 Scala concurrency is built on top of the Java concurrency model. 2. 将每个请求放入一个新的线程 T ...
- Thread Safety线程安全
Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分 如果disabled就选择nts(php_stomp-1.0.9-5.5-nts-vc11-x86.zi ...
- clang的线程安全分析模块 thread safety analysis
介绍 Clang的线程安全分析模块是C++语言的一个扩展,能对代码中潜在的竞争条件进行警告.这种分析是完全静态的(即编译时进行),没有运行时的消耗.当前这个功能还在开发中,但它已经具备了足够的成熟度, ...
- Thread Safety in Java(java中的线程安全)
Thread Safety in Java is a very important topic. Java provide multi-threaded environment support usi ...
- Java Concurrency In Practice -Chapter 2 Thread Safety
Writing thread-safe code is managing access to state and in particular to shared, mutable state. Obj ...
- python3 线程 threading.Thread GIL性能详解(2.3)
python3 线程 threading 最基础的线程的使用 import threading, time value = 0 lock = threading.Lock() def change(n ...
- java并发编程学习: 守护线程(Daemon Thread)
在正式理解这个概念前,先把 守护线程 与 守护进程 这二个极其相似的说法区分开,守护进程通常是为了防止某些应用因各种意外原因退出,而在后台独立运行的系统服务或应用程序. 比如:我们开发了一个邮件发送程 ...
- Java 使用线程方式Thread和Runnable,以及Thread与Runnable的区别
一. java中实现线程的方式有Thread和Runnable Thread: public class Thread1 extends Thread{ @Override public void r ...
- Effective Java 70 Document thread safety
Principle The presence of the synchronized modifier in a method declaration is an implementation det ...
随机推荐
- HTML——UL+CSS设计
截图例如,下面的: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/ ...
- SQLserver2012 tcp/ip 1433port问题解决方法
非常多MSSQL安装完毕后,调用1433(默认port)是失败的,这边具体介绍下解决方法. 一..我们须要在电脑上开启telnet服务,定位问题须要.在cmd下使用telnet,假设报命令不存在说明没 ...
- wamp5中的apache不能启动,80端口被占用
在wamp中apache中的httpd.conf文件中 端口文件设置为8080 #Listen 12.34.56.78:8080Listen 8080
- SQL Server中的查询
本博文简介一下SQL Server中经常使用的几类查询及相关使用的方法. 一.ExecuteScalar方法获取单一值 ExecuteScalar方法是SqlCom ...
- 蚁群算法和简要matlab来源
1 蚁群算法原理 从1991由意大利学者 M. Dorigo,V. Maniezzo 和 A. Colorni 通过模拟蚁群觅食行为提出了一种基于群体的模拟进化算法--蚁群优化.极大关注,蚁群算法的特 ...
- IOS开发-表视图LV3导航控制器
学到这里感觉有点难了,其实这篇文章再草稿箱里放了好久了~ 最近对于学习的热情下降了.这不行-抓紧学习走起! 在这一章节的学习中主要针对导航控制器及表视图来建立多视图的应用, 首先要了解一些概念-- 1 ...
- [Windwos Phone] 实作地图缩放 MapAnimationKind 属性效果
原文:[Windwos Phone] 实作地图缩放 MapAnimationKind 属性效果 [前言] 使用经纬度来定位地图的位置,以及使用 MapAnimationKind 属性来设定地图缩放时的 ...
- Dubbo入门基础与实例讲解(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,0 ...
- Xcode 5.1.1 与 Xcode 6.0.1 共存
Xcode 5.1.1 (下面简称Xcode5)和Xcode 6.0.1(下面简称Xcode6)都是正式版本号.其应用程序文件名称都是"Xcode".假设通过AppStore升级或 ...
- bash组织成树数据结构
君子也非独占,善假于物!bash也因此.昨天晚上,今天早上世界杯很精彩.晚上醒来看到不断地居住的电话.早上没有喝的水开始赞赏在英国和意大利的对决.也TM精彩,最后生下了罗马文化.意大利伊特鲁里亚文化获 ...