Writing Reentrant and Thread-Safe Code
编写可重入和线程安全的代码
In single-threaded processes there is only one flow of control. The code executed by these processes thus need not to be reentrant or thread-safe. In multi-threaded programs, the same functions and the same resources may be accessed concurrently by several flows of control. To protect resource integrity, code written for multi-threaded programs must be reentrant and thread-safe. 单线程的进程中仅有一个控制流。这种进程执行的代码无需可重入或线程安全。在多线程的程序中,同一函数或资源可能被多个控制流并发访问。为保护资源完整性,多线程程序编码必须可重入且线程安全。
This section provides information for writing reentrant and thread-safe programs. It does not cover the topic of writing thread-efficient programs. Thread-efficient programs are efficiently parallelized programs. This can only be done during the design of the program. Existing single-threaded programs can be made thread-efficient, but this requires that they be completely redesigned and rewritten. 本节提供了一些编写可重入和线程安全程序的(指导)信息,但不包括编写线程高效程序的主题。线程高效程序是高效并行化的程序,仅可在程序设计中实现。现有的单线程程序可变得线程高效,但这需要完全地重新设计和重写。
Understanding Reentrance and Thread-Safety
Reentrance and thread-safety are both related to the way functions handle resources. Reentrance and thread-safety are separate concepts: a function can be either reentrant, thread-safe, both, or neither. 可重入和线程安全与函数处理资源的方式有关。可重入和线程安全是两个相互独立的概念:一个函数可以仅是可重入的,可以仅是线程安全的,可以两者皆是或两者皆不是。
Reentrance
A reentrant function does not hold static data over successive calls, nor does it return a pointer to static data. All data is provided by the caller of the function. A reentrant function must not call non-reentrant functions. 可重入函数不能为后续的调用保持静态(或全局)数据,也不能返回指向静态(或全局)数据的指针。函数中用到的所有数据,都应由函数调用者提供(不包括栈上的局部数据)。可重入函数不能调用不可重入的函数。
A non-reentrant function can often, but not always, be identified by its external interface and its usage. For example, the strtok subroutine is not reentrant, because it holds the string to be broken into tokens. The ctime subroutine is also not reentrant; it returns a pointer to static data that is overwritten by each call. 不可重入的函数经常(但不总是)可以通过其外部接口和用法识别。例如strtok子例程是不可重入的,因为它保存着将被分隔为子串的字符串。ctime也是不可重入的,它返回一个指向静态数据的指针,每次调用都会覆盖这些数据。
Thread-Safety
A thread-safe function protects shared resources from concurrent access by locks. Thread-safety concerns only the implementation of a function and does not affect its external interface. 线程安全的函数通过“锁”来保护共享资源不被并发地访问。“线程安全”仅关心函数的实现,而不影响其外部接口。
In C, local variables are dynamically allocated on the stack. Therefore, any function that does not use static data or other shared resources is trivially thread-safe. For example, the following function is thread-safe: 在C中,局部变量在栈上动态分配,因此,任何不使用静态数据和其它共享资源的函数就是最普通的线程安全(函数)。例如,以下函数就是线程安全的:
1 /* thread-safe function */ 2 int diff(int x, int y) 3 { 4 int delta; 5 delta = y - x; 6 if (delta < 0) 7 delta = -delta; 8 return delta; 9 }