第 154 / 187 篇

14.6. 线程安全

说明

本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C14-SharedMemory/thread_safety.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。

14.6. Thread Safety

到目前为止,我们介绍了程序员可以用来确保其多线程程序无论使用多少个线程都一致且正确的同步结构。然而,在任何多线程应用程序中都可以「原样」使用标准 C 库函数,这个假设并不总是安全的。并非 C 库中的所有函数都是线程安全(thread safe)的,即能够由多个线程运行,同时保证结果正确、没有意外的副作用。为了确保我们编写的程序是线程安全的,重要的是使用互斥锁和屏障等同步原语,以保证无论线程数如何变化,多线程程序都一致且正确。

So far, we have covered synchronization constructs that programmers can use to ensure that their multithreaded programs are consistent and correct regardless of the number of threads employed. However, it is not always safe to make the assumption that standard C library functions can be used “as is” in the context of any multithreaded application. Not all functions in the C library are thread safe, or capable of being run by multiple threads while guaranteeing a correct result without unintended side effects. To ensure that the programs we write are thread safe, it is important to use synchronization primitives like mutexes and barriers to enforce that multithreaded programs are consistent and correct regardless of how the number of threads varies.

与线程安全密切相关的另一个概念是可重入性(re-entrancy)。所有线程安全的代码都是可重入的;然而,并非所有可重入的代码都是线程安全的。如果一个函数可以被某个函数重新执行或部分执行而不引起问题,它就是可重入的(re-entrant)。按照定义,可重入的代码确保对程序全局状态的访问总能使全局状态保持一致。尽管可重入性常常被(错误地)当作线程安全的同义词,但在一些特殊情况下,可重入的代码并不是线程安全的。

Another closely related concept related to thread safety is re-entrancy. All thread safe code is re-entrant; however, not all re-entrant code is thread safe. A function is re-entrant if it can be re-executed/partially executed by a function without causing issue. By definition, re-entrant code ensures that accesses to the global state of a program always result in that global state remaining consistent. While re-entrancy is often (incorrectly) used as a synonym for thread safety, there are special cases for which re-entrant code is not thread safe.

译注 · 线程安全不蕴含可重入

「所有线程安全的代码都是可重入的」这一说法并不成立,两者互不蕴含。一个反例是 printf:按 printf(3) 手册,它在 glibc 中被标为线程安全(MT-Safe);但 signal-safety(7) 手册专门以 stdio 函数为例解释了为什么不能在信号处理程序中调用它:如果主程序正执行到 printf 的一半(缓冲区和相关变量只更新了一部分)时被信号打断,而信号处理程序又调用 printf,就会出问题,即它不是可重入(异步信号安全)的。用互斥锁保证线程安全的函数尤其如此:同一线程在持有锁时被信号打断、再次进入该函数,就会因为试图获取自己已持有的锁而死锁。

编写多线程代码时,要核实所用的 C 库函数确实是线程安全的。幸运的是,线程不安全的 C 库函数列表相当短。The Open Group 好心地维护着一份线程不安全函数的列表。

When writing multithreaded code, verify that the C library functions used are indeed thread safe. Fortunately, the list of thread unsafe C library functions is fairly small. The Open Group kindly maintains a list of thread-unsafe functions.

14.6.1. 修正线程安全问题

14.6.1. Fixing Issues of Thread Safety

同步原语是修正线程安全问题最常用的方法。然而,不知不觉地使用了线程不安全的 C 库函数,可能会引发难以察觉的问题。我们来看 countsElem 函数的一个稍作修改的版本,名为 countElemsStr,它试图统计给定字符串中各数字出现的频次,其中各个数字之间用空格分隔。下面的程序为简洁起见做了删节;这个程序的完整源代码见:countElemsStr.c。

Synchronization primitives are the most common way to fix issues related to thread safety. However, unknowingly using thread-unsafe C library functions can cause subtle issues. Let’s look at a slightly modified version of our countsElem function called countElemsStr, which attempts to count the frequency of digits in a given string, where each digit is separated by spaces. The following program has been edited for brevity; the full source of this program is available at: countElemsStr.c.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* computes the frequency of all the elements in the input string and stores
* the associated counts of each element in the array called counts. */
void countElemsStr(int *counts, char *input_str) {
int val, i;
char *token;
token = strtok(input_str, " ");
while (token != NULL) {
val = atoi(token);
counts[val] = counts[val] + 1;
token = strtok(NULL, " ");
}
}

/* main function:
* calls countElemsStr on a static string and counts up all the digits in
* that string. */
int main( int argc, char **argv ) {
//lines omitted for brevity, but gets user defined length of string

//fill string with n digits
char *inputString = calloc(length * 2, sizeof(char));
fillString(inputString, length * 2);

countElemsStr(counts, inputString);

return 0;
}

countElemsStr 函数使用 strtok 函数(在我们对字符串的讨论中考察过)解析字符串中的每个数字(存放在 token 中),然后把它转换成整数,并对 counts 数组进行相应的更新。

The countElemsStr function uses the strtok function (as examined in our discussion on strings) to parse each digit (stored in token) in the string, before converting it to an integer and making the associated updates in the counts array.

在 100,000 个元素上编译并运行这个程序,得到下面的输出:

Compiling and running this program on 100,000 elements yields the following output:

1
2
3
4
5
$ gcc -o countElemsStr countElemsStr.c

$ ./countElemsStr 100000 1
contents of counts array:
9963 9975 9953 10121 10058 10017 10053 9905 9915 10040

现在,我们来看 countElemsStr 的多线程版本(程序的完整源代码可在这里查看):

Now, let’s take a look at a multithreaded version of countElemsStr (full source of the program viewable here):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* parallel version of countElemsStr (First cut):
* computes the frequency of all the elements in the input string and stores
* the associated counts of each element in the array called counts
*/
void *countElemsStr(void *args) {
//parse args
struct t_arg *myargs = (struct t_arg *)args;
//omitted for brevity

//local variables
int val, i;
char *token;
int local_counts[MAX] = {0};

//compute local start and end values and chunk size:
//omitted for brevity

//tokenize values
token = strtok(input_str + start, " ");
while (token != NULL) {
val = atoi(token); //convert to an int
local_counts[val] = local_counts[val] + 1; //update associated counts
token = strtok(NULL, " ");
}

pthread_mutex_lock(&mutex);
for (i = 0; i < MAX; i++) {
counts[i] += local_counts[i];
}
pthread_mutex_unlock(&mutex);

return NULL;
}

在这个版本的程序中,每个线程处理 input_str 所引用字符串的不同部分。local_counts 数组确保大部分写操作发生在局部存储上。使用互斥锁来确保不会有两个线程同时写共享变量 counts。

In this version of the program, each thread processes a separate section of the string referenced by input_str. The local_counts array ensures that the bulk of the write operations occur to local storage. A mutex is employed to ensure that no two threads write to the shared variable counts.

然而,编译并运行这个程序,得到的结果如下:

However, compiling and running this program yields the following results:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ gcc -o countElemsStr_p countElemsStr_p.c -pthread

$ ./countElemsStr_p 100000 1 1
contents of counts array:
9963 9975 9953 10121 10058 10017 10053 9905 9915 10040

$ ./countElemsStr_p 100000 1 2
contents of counts array:
498 459 456 450 456 471 446 462 450 463

$ ./countElemsStr_p 100000 1 4
contents of counts array:
5038 4988 4985 5042 5056 5013 5025 5035 4968 5065

尽管对 counts 数组的访问都用互斥锁保护起来了,但几次运行的结果却大不相同。出现这个问题,是因为 countsElemsStr 函数不是线程安全的,原因在于字符串库函数 strtok 不是线程安全的!访问 OpenGroup 网站可以确认,strtok 就在线程不安全函数的列表上。

Even though mutex locks are used around accesses to the counts array, the results from separate runs are radically different. This issue arises because the countsElemsStr function is not thread safe, because the string library function strtok is not thread safe! Visiting the OpenGroup website confirms that strtok is on the list of thread-unsafe functions.

要修正这个问题,只需把 strtok 换成它的线程安全版本 strtok_r。在后一个函数中,用最后一个参数——一个指针——来帮助线程记录它在字符串中解析到了哪里。下面是用 strtok_r 修正后的函数(完整源代码见 countsElemsStr_p_v2.c):

To fix this issue, it suffices to replace strtok with its thread-safe alternative, strtok_r. In the latter function, a pointer is used as the last parameter to help the thread keep track of where in the string it is parsing. Here is the fixed function with strtok_r (full source code here (countsElemsStr_p_v2.c):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* parallel version of countElemsStr (First cut):
* computes the frequency of all the elements in the input string and stores
* the associated counts of each element in the array called counts */
void* countElemsStr(void* args) {
//parse arguments
//omitted for brevity

//local variables
int val, i;
char * token;
int local_counts[MAX] = {0};
char * saveptr; //for saving state of strtok_r

//compute local start and end values and chunk size:
//omitted for brevity

//tokenize values
token = strtok_r(input_str+start, " ", &saveptr);
while (token != NULL) {
val = atoi(token); //convert to an int
local_counts[val] = local_counts[val]+1; //update associated counts
token = strtok_r(NULL, " ", &saveptr);
}

pthread_mutex_lock(&mutex);
for (i = 0; i < MAX; i++) {
counts[i]+=local_counts[i];
}
pthread_mutex_unlock(&mutex);

return NULL;
}

这个版本代码中唯一的改动,是声明了字符指针 saveptr,并把所有 strtok 都替换成了 strtok_r。做了这些改动后重新运行代码,得到下面的输出:

The only change in this version of the code is the declaration of the character pointer saveptr and replacing all instances of strtok with strtok_r. Rerunning the code with these changes yields the following output:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ gcc -o countElemsStr_p_v2 countElemsStr_p_v2.c -pthread

$ ./countElemsStr_p_v2 100000 1 1
contents of counts array:
9963 9975 9953 10121 10058 10017 10053 9905 9915 10040

$ ./countElemsStr_p_v2 100000 1 2
contents of counts array:
9963 9975 9953 10121 10058 10017 10053 9905 9915 10040

$ ./countElemsStr_p_v2 100000 1 4
contents of counts array:
9963 9975 9953 10121 10058 10017 10053 9905 9915 10040

现在,程序每次运行都得到相同的结果。把 saveptr 与 strtok_r 结合使用,确保每个线程在解析字符串时都能独立地记录自己的位置。

Now the program produces the same result for every run. The use of saveptr in conjunction with strtok_r ensures that each thread can independently track their location when parsing the string.

本节的要点是:编写多线程应用程序时,应当始终查看 C 中线程不安全函数的列表。这样做能让程序员在编写和调试多线程应用程序时省去许多烦恼和挫折。

The takeaway from this section is that one should always check the list of thread-unsafe functions in C when writing multithreaded applications. Doing so can save the programmer a lot of heartache and frustration when writing and debugging threaded applications.