Thread safety is a computer programming concept applicable in the context of multi-threaded programs. A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads.
A computer program or routine is described as reentrant if the routine can be re-entered while it is already running (i.e it can be safely executed concurrently).
from:wikipedia
以下情况意味着线程不安全:
以下情况线程安全:
- accessing global variables or the heap //访问全局变量和堆内存
- allocating/reallocating/freeing resources that have global scope (files, sub-processes, etc.) //访问(分配,重新分配,释放)全局资源 (最常见的是I/O资源)
- indirect accesses through handles or pointers //通过指针间接访问资源
- any visible side-effect (e.g., access to volatile variables in the C programming language) //执行任何有副作用的代码 , 大部分也是IO类的
Such a sub-routine is sometimes called a "pure function", and is much like a mathematical function. //所谓函数式编程是也
- the only variables it uses are from the stack //只使用栈内存
- execution depends only on the arguments passed in, and //代码运行只依赖传入参数
- the only subroutines it calls have the same properties. //或者这段代码所调用的其它函数的运行结果只取决于传入参数
Mutual exclusion //使用锁对并发控制在访问临界资源时串行化Access to shared data is serialized using mechanisms that ensure only one thread reads or writes the shared data at any time. Great care is required if a piece of code accesses multiple shared pieces of data—problems include race conditions, deadlocks, livelocks, starvation, and various other ills enumerated in many operating systems textbooks.Thread-local storage //线程局部存储,绕开全局变量的一种方式Variables are localized so that each thread has its own private copy. These variables retain their values across subroutine and other code boundaries, and are thread-safe since they are local to each thread, even though the code which accesses them might be reentrant.Atomic operations //原子操作,一般而言对于i++这样的操作,都不是原子操作,所谓原子操作,是不可能被线程切换中断的操作。现代计算机的汇编指令,一般会有一些支持原子加减之类的操作Shared data are accessed by using atomic operations which cannot be interrupted by other threads. This usually requires using special machine language instructions, which might be available in a runtime library. Since the operations are atomic, the shared data are always kept in a valid state, no matter what other threads access it. Atomic operations form the basis of many thread locking mechanisms.
To be reentrant, a computer program or routine:
- Must hold no static (or global) non-constant data. //无静态或全局变量
- Must not return the address to static (or 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 modify its own code.[1] (unless executing in its own unique thread storage) //除了使用线程局部存储外,不能修改自身代码(数据) ,( 很多编程语言可以在运行时修改自身代码), C/C++也行,不过很tricky
- Must not call non-reentrant computer programs or routines. //不能调用非可重入的函数 (典型的递归定义计算机概念)
可重入与线程安全两个概念都关系到函数处理资源的方式。但是,他们有一定的区别。可重入概念会影响函数的外部接口,而线程安全只关心函数的实现。
- 大多数情况下,要将不可重入函数改为可重入的,需要修改函数接口,使得所有的数据都通过函数的调用者提供。
因此,相对线程安全来说,可重入性是更基本的特性,它可以保证线程安全:即,所有的可重入函数都是线程安全的,但并非所有的线程安全函数都是可重入的。
- 要将非线程安全的函数改为线程安全的,则只需要修改函数的实现部分。一般通过加入同步机制以保护共享的资源,使之不会被几个进程同时访问。
from:http://doc.qt.nokia.com/4.6/threads-reentrancy.html#reentrant
A thread-safe function can be called simultaneously from multiple threads, even when the invocations use shared data, because all references to the shared data are serialized.A reentrant function can also be called simultaneously from multiple threads, but only if each invocation uses its own data.
Hence, a thread-safe function is always reentrant, but a reentrant function is not always thread-safe.
C++ classes are often reentrant, simply because they only access their own member data. Any thread can call a member function on an instance of a reentrant class, as long as no other thread can call a member function on the same instance of the class at the same time.
原帖由 woodheadz 于 24-12-2009 03:08 发表
有一种情况不太清楚该怎么算:
如果有一个函数接收一个资源的指针作为参数并在函数内修改该资源, 这个函数貌似没有违反楼上”可重入“的几个特征吧?
原帖由 woodheadz 于 24-12-2009 02:08 发表
有一种情况不太清楚该怎么算:
如果有一个函数接收一个资源的指针作为参数并在函数内修改该资源, 这个函数貌似没有违反楼上”可重入“的几个特征吧?
欢迎光临 FreeOZ论坛 (https://www.freeoz.org/ibbs/) | Powered by Discuz! X3.2 |