说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C14-SharedMemory/posix.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
14.2. Hello Threading! Writing Your First Multithreaded Program
在本节中,我们考察无处不在的 POSIX 线程库 Pthreads。POSIX 是 Portable Operating System Interface(可移植操作系统接口)的缩写。它是一项 IEEE 标准,规定了 UNIX 系统的外观、行为和使用感受。POSIX 线程 API 在几乎所有类 UNIX 操作系统上都可以使用,这些系统要么完全符合该标准,要么在很大程度上符合。所以,如果你在一台 Linux 机器上用 POSIX 线程编写并行代码,它肯定能在其他 Linux 机器上运行,并且很可能也能在运行 macOS 或其他 UNIX 变体的机器上运行。
In this section, we examine the ubiquitous POSIX thread library Pthreads. POSIX is an acronym for Portable Operating System Interface. It is an IEEE standard that specifies how UNIX systems look, act, and feel. The POSIX threads API is available on almost all UNIX-like operating systems, each of which meets the standard in its entirety or to some great degree. So, if you write parallel code using POSIX threads on a Linux machine, it will certainly work on other Linux machines, and it will likely work on machines running macOS or other UNIX variants.
我们先来分析一个「Hello World」Pthreads 示例程序(hellothreads.c)。为简洁起见,清单中省略了错误处理,不过可下载的版本中包含错误处理的示例。
Let’s begin by analyzing an example “Hello World” Pthreads program (hellothreads.c). For brevity, we have excluded error handling in the listing, though the downloadable version contains sample error handling.
1 | #include <stdio.h> |
我们把这个程序拆成小块来考察。
Let’s examine this program in smaller components.
- 注意其中包含了
pthread.h头文件,它声明了pthread的类型和函数。
- Notice the inclusion of the
pthread.hheader file, which declarespthreadtypes and functions.
- 接下来,
HelloWorld函数定义了线程函数(thread function),我们稍后会把它传给pthread_create。线程函数相当于工作线程(被创建的线程)的main函数:线程从线程函数的开头开始执行,到达线程函数的末尾时终止。每个线程都使用自己私有的执行状态(即它自己的栈内存和寄存器值)来执行线程函数。还要注意,线程函数的类型是void*。在这里使用匿名指针,让程序员能编写处理不同类型参数和返回值的线程函数。
- Next, the
HelloWorldfunction defines the thread function that we later pass topthread_create. A thread function is analogous to amainfunction for a worker (created) thread — a thread begins execution at the start of its thread function and terminates when it reaches the end. Each thread executes the thread function using its private execution state (i.e., its own stack memory and register values). Note also that the thread function is of typevoid*. Specifying an anonymous pointer in this context allows programmers to write thread functions that deal with arguments and return values of different types.
- 最后,在
main函数中,主线程先初始化程序状态,然后创建并合并(join)工作线程。
- Lastly, in the
mainfunction, the main thread initializes the program state before creating and joining the worker threads.
14.2.1. 创建与合并线程
14.2.1. Creating and Joining Threads
程序一开始是一个单线程进程。执行 main 函数时,它读取要创建的线程数,并为两个数组分配内存:thread_array 和 thread_ids。thread_array 数组存放所创建的每个线程的地址集合。thread_ids 数组存放传给每个线程的参数集合。在这个例子中,传给每个线程的是它的序号(rank,即 ID,用 thread_ids[i] 表示)的地址。
The program first starts as a single-threaded process. As it executes the
mainfunction, it reads the number of threads to create, and it allocates memory for two arrays:thread_arrayandthread_ids. Thethread_arrayarray contains the set of addresses for each thread created. Thethread_idsarray stores the set of arguments that each thread is passed. In this example, each thread is passed the address of its rank (or ID, represented bythread_ids[i]).
所有预备变量都分配并初始化之后,main 线程执行多线程的两个主要步骤:
After all the preliminary variables are allocated and initialized, the
mainthread executes the two major steps of multithreading:
- 创建(creation)步骤:主线程派生出一个或多个工作线程。派生之后,每个工作线程在自己的执行上下文中运行,与其他线程以及系统上的其他进程并发执行。
- The creation step, in which the main thread spawns one or more worker threads. After being spawned, each worker thread runs within its own execution context concurrently with the other threads and processes on the system.
- 合并(join)步骤:主线程等待所有工作线程完成,然后再作为单线程进程继续执行。合并一个已终止的线程会释放该线程的执行上下文和资源。试图合并一个尚未终止的线程会使调用者阻塞,直到该线程终止,这与进程的 wait 函数的语义类似。
- The join step, in which the main thread waits for all the workers to complete before proceeding as a single-thread process. Joining a thread that has terminated frees the thread’s execution context and resources. Attempting to join a thread that hasn’t terminated blocks the caller until the thread terminates, similar to the semantics of the wait function for processes.
Pthreads 库提供了用于创建线程的 pthread_create 函数和用于合并线程的 pthread_join 函数。pthread_create 函数的签名如下:
The Pthreads library offers a
pthread_createfunction for creating threads and apthread_joinfunction for joining them. Thepthread_createfunction has the following signature:
1 | pthread_create(pthread_t *thread, const pthread_attr_t *attr, |
这个函数接收一个指向线程结构(类型为 pthread_t)的指针、一个指向属性结构的指针(通常设为 NULL)、线程应当执行的函数的名字,以及线程函数开始时要传给它的参数数组。
The function takes a pointer to a thread struct (of type
pthread_t), a pointer to an attribute struct (normally set toNULL), the name of the function the thread should execute, and the array of arguments to pass to the thread function when it starts.
Hello World 程序在 main 函数中这样调用 pthread_create:
The Hello World program calls
pthread_createin themainfunction using:
1 | pthread_create(&thread_array[i], NULL, HelloWorld, &thread_ids[i]); |
其中:
Here:
&thread_array[i]包含线程 i 的地址。pthread_create函数分配一个pthread_t线程对象,并把它的地址存放在这个位置,让程序员以后能引用这个线程(例如在合并它时)。
&thread_array[i]contains the address of thread i. Thepthread_createfunction allocates apthread_tthread object and stores its address at this location, enabling the programmer to reference the thread later (e.g., when joining it).
译注
更准确地说,pthread_t 的存储空间是调用者提供的(就是数组元素 thread_array[i]),pthread_create 并不另外「分配对象再存地址」。按 pthread_create(3) 手册,调用成功时,它把新线程的 ID 存到 thread 参数所指向的缓冲区中,之后其他 pthreads 函数就用这个标识符来引用该线程。同理,上文说 thread_array 存放的是各线程的「地址」,实为各线程的 ID;第四个参数也是单个 void * 指针,而不是「参数数组」。
NULL表示线程应当以默认属性创建。在大多数程序中,把第二个参数保留为NULL是安全的。
NULLspecifies that the thread should be created with default attributes. In most programs, it is safe to leave this second parameter asNULL.
HelloWorld指定被创建的线程应当执行的线程函数。这个函数的行为就像线程的「main」函数。对于任意一个线程函数(例如function),它的原型必须符合void * function(void *)的形式。
HelloWorldnames the thread function that the created thread should execute. This function behaves like the “main” function for the thread. For an arbitrary thread function (e.g.,function), its prototype must match the formvoid * function(void *).
&thread_ids[i]指定要传给线程 i 的参数的地址。在这里,thread_ids[i]包含一个表示线程 ID 的long。由于pthread_create的最后一个参数必须是指针,我们传入的是线程 ID 的地址。
&thread_ids[i]specifies the address of the arguments to be passed to thread i. In this case,thread_ids[i]contains a singlelongrepresenting the thread’s ID. Since the last argument topthread_createmust be a pointer, we pass the address of the thread’s ID.
为了生成多个执行 HelloWorld 线程函数的线程,程序给每个线程分配一个唯一的 ID,并在一个 for 循环中创建各个线程:
To generate several threads that execute the
HelloWorldthread function, the program assigns each thread a unique ID and creates each thread within aforloop:
1 | for (i = 0; i < nthreads; i++) { |
操作系统负责调度每个被创建线程的执行;用户不能对线程的执行顺序做任何假设。
The OS schedules the execution of each created thread; the user cannot make any assumption on the order in which the threads will execute.
pthread_join 函数挂起其调用者的执行,直到它所引用的线程终止。它的签名是:
The
pthread_joinfunction suspends the execution of its caller until the thread it references terminates. Its signature is:
1 | pthread_join(pthread_t thread, void **return_val) |
pthread_join 接收一个 pthread_t 结构作为输入,指明要等待哪个线程,还有一个可选的指针参数,指定线程的返回值应当存放在哪里。
The
pthread_jointakes as input apthread_tstruct, indicating which thread to wait on, and an optional pointer argument that specifies where the thread’s return value should be stored.
Hello World 程序在 main 中这样调用 pthread_join:
The Hello World program calls
pthread_joininmainusing:
1 | pthread_join(thread_array[t], NULL); |
这一行表示主线程必须等待线程 t 终止。把 NULL 作为第二个参数传入,表示程序不使用线程的返回值。
This line indicates that the main thread must wait on the termination of thread
t. PassingNULLas the second argument indicates that the program does not use the thread’s return value.
在前面的程序中,main 在一个循环中调用 pthread_join,因为在 main 函数继续清理内存并终止进程之前,所有工作线程都需要终止:
In the previous program,
maincallspthread_joinin a loop because all of the worker threads need to terminate before themainfunction proceeds to clean up memory and terminate the process:
1 | for (i = 0; i < nthreads; i++) { |
14.2.2. 线程函数
14.2.2. The Thread Function
在前面的程序中,每个派生出的线程都打印出 Hello world! I am thread n,其中 n 是线程的唯一 id。线程打印出消息后就终止了。我们来仔细看看 HelloWorld 函数:
In the previous program, each spawned thread prints out
Hello world! I am thread n, wherenis the thread’s unique id. After the thread prints out its message, it terminates. Let’s take a closer look at theHelloWorldfunction:
1 | void *HelloWorld(void *id) { |
回想一下,pthread_create 通过 thread_args 参数把参数传给线程函数。在 main 中的 pthread_create 函数里,Hello World 程序指定这个参数实际上就是线程的 ID。注意,HelloWorld 的参数必须声明为通用指针,即匿名指针(void *)。Pthreads 库使用 void *,不规定参数类型,从而让 pthread_create 更加通用。对程序员来说,void * 有点不方便,因为使用前必须进行类型转换。在这里,我们知道参数的类型是 long *,因为我们在 main 中传给 pthread_create 的就是这个类型。因此,我们可以安全地把这个值转换为 long *,并对指针解引用来访问 long 值。许多并行程序都遵循这样的结构。
Recall that
pthread_createpasses the arguments to the thread function using thethread_argsparameter. In thepthread_createfunction inmain, the Hello World program specified that this parameter is in fact the thread’s ID. Note that the parameter toHelloWorldmust be declared as a generic or anonymous pointer (void *). The Pthreads library usesvoid *to makepthread_createmore general purpose by not prescribing a parameter type. As a programmer, thevoid *is mildly inconvenient given that it must be recast before use. Here, we know the parameter is of typelong *because that’s what we passed topthread_createinmain. Thus, we can safely cast the value as along *and dereference the pointer to access thelongvalue. Many parallel programs follow this structure.
与线程函数的参数类似,Pthreads 库也通过规定另一个 void * 来避免规定线程函数的返回类型:程序员可以从线程函数返回任何指针。如果程序需要访问线程的返回值,可以通过 pthread_join 的第二个参数取得。在我们的例子中,线程不需要返回值,所以它只是返回一个 NULL 指针。
Similar to the thread function’s parameter, the Pthreads library avoids prescribing the thread function’s return type by specifying another
void *— the programmer is free to return any pointer from the thread function. If the program needs to access the thread’s return value, it can retrieve it via the second argument topthread_join. In our example, the thread has no need to return a value, so it simply returns aNULLpointer.
14.2.3. 运行代码
14.2.3. Running the Code
下面的命令展示了如何用 GCC 编译 hellothreads.c。构建 Pthreads 应用程序时,需要给 GCC 传入 -pthread 链接选项,以确保能使用 Pthreads 的函数和类型:
The command that follows shows how to use GCC to compile hellothreads.c. Building a Pthreads application requires that the
-pthreadlinker flag be passed to GCC to ensure that the Pthreads functions and types are accessible:
1 | $ gcc -o hellothreads hellothreads.c -pthread |
不带命令行参数运行程序,会得到一条用法提示:
Running the program without a command line argument results in a usage message:
1 | $ ./hellothreads |
用四个线程运行程序,得到下面的输出:
Running the program with four threads yields the following output:
1 | $ ./hellothreads 4 |
注意,每个线程都打印出它唯一的 ID 号。在这次运行中,线程 1 的输出最先显示,接着是线程 2、3 和 0。如果再次运行程序,输出的显示顺序可能会不同:
Notice that each thread prints its unique ID number. In this run, thread 1’s output displays first, followed by threads 2, 3, and 0. If we run the program again, we may see the output displayed in a different order:
1 | $ ./hellothreads 4 |
回想一下,线程的执行顺序由操作系统的调度器决定。从用户的角度看,这个顺序实际上是随机的,因为它受到许多用户无法控制的、不断变化的因素影响(例如可用的系统资源、系统收到的输入或操作系统的调度)。由于所有线程彼此并发运行,并且每个线程都执行一次对 printf 的调用(打印到 stdout),所以最先打印到 stdout 的线程,它的输出就最先出现。后续的执行可能(也可能不)产生不同的输出。
Recall that the operating system’s scheduler determines the thread execution order. From a user’s perspective, the order is effectively random due to being influenced by many factors that vary outside the user’s control (e.g., available system resources, the system receiving input, or OS scheduling). Since all threads are running concurrently with one another and each thread executes a call to
printf(which prints tostdout), the first thread that prints tostdoutwill have its output show up first. Subsequent executions may (or may not) result in different output.
原书警示 · 线程的执行顺序(Thread Execution Order)
你绝不应该对线程的执行顺序做任何假设。如果程序的正确性要求线程按特定的顺序运行,就必须在程序中加入同步,防止线程在不该运行的时候运行。
You should never make any assumptions about the order in which threads will execute. If the correctness of your program requires that threads run in a particular order, you must add synchronization to your program to prevent threads from running when they shouldn’t.
14.2.4. 再看标量乘法
14.2.4. Revisiting Scalar Multiplication
我们来探讨如何为上一节的标量乘法程序编写一个多线程实现。回想一下,并行化 scalar_multiply 的一般策略是:
Let’s explore how to create a multithreaded implementation of the scalar multiplication program from the previous section. Recall that our general strategy for parallelizing
scalar_multiplyis to:
- 创建多个线程,
- Create multiple threads,
- 给每个线程分配输入数组的一个子集,
- Assign each thread a subset of the input array,
- 让每个线程把它所分到的数组子集中的元素乘以
s。
- Instruct each thread to multiply the elements in its array subset by
s.
下面是完成这项任务的线程函数。注意,我们把 array、length 和 s 移到了程序的全局作用域中。
The following is a thread function that accomplishes this task. Notice that we have moved
array,length, andsto the global scope of the program.
1 | long *array; //allocated in main |
我们把它分成几部分来看。回想一下,第一步是给每个线程分配数组的一部分。下面几行完成这项任务:
Let’s break this down into parts. Recall that the first step is to assign each thread a component of the array. The following lines accomplish this task:
1 | long chunk = length / nthreads; |
变量 chunk 存放分配给每个线程的元素个数。为了确保每个线程分到的工作量大致相同,我们先把块大小设为元素个数除以线程数,即 length / nthreads。
The variable
chunkstores the number of elements that each thread is assigned. To ensure that each thread gets roughly the same amount of work, we first set the chunk size to the number of elements divided by the number of threads, orlength / nthreads.
接下来,我们给每个线程分配一个不同的元素范围来处理。每个线程利用 chunk 大小和它唯一的线程 ID,算出自己范围的 start 和 end 下标。
Next, we assign each thread a distinct range of elements to process. Each thread computes its range’s
startandendindex using thechunksize and its unique thread ID.
例如,在四个线程(ID 为 0–3)处理一个有 1 亿个元素的数组时,每个线程负责处理一个 2500 万个元素的 chunk。把线程 ID 纳入计算,就能给每个线程分配输入中一个唯一的子集。
For example, with four threads (with IDs 0-3) operating over an array with 100 million elements, each thread is responsible for processing a 25 million element
chunk. Incorporating the thread ID assigns each thread a unique subset of the input.
接下来的两行处理 length 不能被线程数整除的情况:
The next two lines account for the case in which
lengthis not evenly divisible by the number of threads:
1 | if (*myid == nthreads - 1) { |
假设我们指定的是三个而不是四个线程。名义上的块大小将是 33,333,333 个元素,剩下一个元素没有分配出去。前面例子中的代码会把剩下的元素分配给最后一个线程。
Suppose that we specified three rather than four threads. The nominal chunk size would be 33,333,333 elements, leaving one element unaccounted for. The code in the previous example would assign the remaining element to the last thread.
原书注 · 构造均衡的输入(Creating balanced input)
刚才展示的分块代码并不完美。在线程数不能整除输入规模的情况下,余数被分配给最后一个线程。考虑一次示例运行,其中数组有 100 个元素,指定了 12 个线程。名义上的块大小是 8,余数是 4。用这个示例代码,前 11 个线程各分到 8 个元素,而最后一个线程分到 12 个元素。因此,最后一个线程要比其他线程多做 50% 的工作。对这个例子来说,一种可能更好的分块方式是让前 4 个线程各处理 9 个元素,而后 8 个线程各处理 8 个元素。这样就能让输入在各线程之间实现更好的负载均衡(load balancing)。
The chunking code just shown is imperfect. In the case where the number of threads does not evenly divide the input, the remainder is assigned to the last thread. Consider a sample run in which the array has 100 elements, and 12 threads are specified. The nominal chunk size would be 8, and the remainder would be 4. With the example code, the first 11 threads will each have 8 assigned elements, whereas the last thread will be assigned 12 elements. Consequently, the last thread performs 50% more work than the other threads. A potentially better way to chunk this example is to have the first 4 threads process 9 elements each, while the last 8 threads process 8 elements each. This will result in better load balancing of the input across the threads.
算出了合适的局部 start 和 end 下标之后,每个线程就可以对自己负责的那部分数组执行标量乘法了。scalar_multiply 函数的最后一部分完成这项工作:
With an appropriate local
startandendindex computed, each thread is now ready to perform scalar multiplication on its component of the array. The last portion of thescalar_multiplyfunction accomplishes this:
1 | for (i = start; i < end; i++) { |
14.2.5. 改进标量乘法:多个参数
14.2.5. Improving Scalar Multiplication: Multiple Arguments
前面那个实现的一个关键弱点是大量使用了全局变量。我们最初对全局变量的讨论表明,全局变量虽然有用,但在 C 中一般应当避免使用。为了减少程序中的全局变量,一种解决办法是像下面这样在全局作用域中声明一个 t_arg 结构体:
A key weakness of the previous implementation is the wide use of global variables. Our original discussion of global variables showed that although useful, global variables should generally be avoided in C. To reduce the number of global variables in the program, one solution is to declare a
t_argstruct as follows in the global scope:
1 | struct t_arg { |
我们的 main 函数除了分配 array 并设置局部变量 length、nthreads 和 s(缩放因子)之外,还要分配一个 t_arg 记录数组:
Our main function would, in addition to allocating
arrayand setting local variableslength,nthreads, ands(our scaling factor), allocate an array oft_argrecords:
1 | long nthreads = strtol(argv[1], NULL, 10); //get number of threads |
之后在 main 中调用 pthread_create 时,把线程相应的 t_args 结构体作为参数传入:
Later in
main, whenpthread_createis called, the thread’s associatedt_argsstruct is passed as an argument:
1 | for (i = 0; i < nthreads; i++){ |
最后,我们的 scalar_multiply 函数将如下所示:
Lastly, our
scalar_multiplyfunction would look like the following:
1 | void * scalar_multiply(void* args) { |
完整实现这个程序留给读者作为练习。请注意,为简洁起见,这里省略了错误处理。
Implementing this program fully is an exercise we leave to the reader. Please note that error handling has been omitted for the sake of brevity.