说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C14-SharedMemory/summary.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
14.8. Summary
本章概述了多核处理器以及如何为它们编程。具体来说,我们介绍了 POSIX 线程(即 Pthreads)库,以及如何用它编写正确的多线程程序来加快单线程程序的性能。POSIX 和 OpenMP 这样的库采用共享内存(shared memory)通信模型,因为线程在一个公共的内存空间中共享数据。
This chapter provided an overview of multicore processors and how to program them. Specifically, we cover the POSIX threads (or Pthreads) library and how to use it to create correct multithreaded programs that speed up a single-threaded program’s performance. Libraries like POSIX and OpenMP utilize the shared memory model of communication, as threads share data in a common memory space.
要点
Key Takeaways
线程是并发程序的基本单位
为了并行化一个串行程序,程序员使用称为线程的轻量级结构。对于一个特定的多线程进程,每个线程都有自己的栈内存,但共享进程的程序数据、堆和指令。与进程一样,线程在 CPU 上的运行是不确定的(nondeterministic,即执行顺序每次运行都会变化,哪个线程被分配到哪个核心由操作系统决定)。
Threads are the fundamental unit of concurrent programs
To parallelize a serial program, programmers utilize lightweight constructs known as threads. For a particular multithreaded process, each thread has its own allocation of stack memory, but shares the program data, heap and instructions of the process. Like processes, threads run nondeterministically on the CPU (i.e., the order of execution changes between runs, and which thread is assigned to which core is left up to the operating system).
同步结构确保程序正确运行
共享内存的一个后果是,线程可能无意中覆盖驻留在共享内存中的数据。每当两个操作错误地更新一个共享值时,就可能发生竞态条件。当这个共享值是数据时,就可能出现一种特殊的竞态条件,称为数据竞争。同步结构(互斥锁、信号量等)通过确保线程在更新共享变量时一次一个地执行,来帮助保证程序的正确性。
Synchronization constructs ensure that programs work correctly
A consequence of shared memory is that threads can accidentally overwrite data residing in shared memory. A race condition can occur whenever two operations incorrectly update a shared value. When that shared value is data, a special type of race condition called a data race can arise. Synchronization constructs (mutexes, semaphores, etc.) help to guarantee program correctness by ensuring that threads execute one at a time when updating shared variables.
使用同步结构时要谨慎
同步本质上会在原本并行的程序中引入串行计算的点。因此,重要的是要清楚如何使用同步概念。必须原子地运行的那组操作称为临界区。如果临界区太大,线程就会串行执行,运行时间得不到任何改善。同步结构用得马虎,就可能无意中出现死锁之类的情况。一个好的策略是让线程尽可能使用局部变量,只在必要时才更新共享变量。
Be mindful when using synchronization constructs
Synchronization inherently introduces points of serial computation in an otherwise parallel program. It is therefore important to be aware of how one uses synchronization concepts. The set of operations that must run atomically is referred to as a critical section. If a critical section is too big, the threads will execute serially, yielding no improvement in runtime. Use synchronization constructs sloppily, and situations like deadlock may inadvertently arise. A good strategy is to have threads employ local variables as much as possible and update shared variables only when necessary.
并非程序的所有部分都可以并行化
有些程序必然包含很大的串行部分,这可能会妨碍多线程程序在多个核心上的性能(例如阿姆达尔定律)。即使程序中可并行的比例很高,加速比也很少是线性的。在确定程序的性能时,我们也鼓励读者考察效率和可扩展性等其他指标。
Not all components of a program are parallelizable
Some programs necessarily have large serial components that can hinder a multithreaded program’s performance on multiple cores (e.g., Amdahl’s Law). Even when a high percentage of a program is parallelizable, speedup is rarely linear. Readers are also encouraged to look at other metrics such as efficiency and scalability when ascertaining the performance of their programs.
延伸阅读
Further Reading
本章旨在让读者初步领略关于线程的并发话题,绝不是面面俱到的。要进一步学习用 POSIX 线程和 OpenMP 编程,请查看劳伦斯·利弗莫尔国家实验室的 Blaise Barney 编写的优秀的 Pthreads 和 OpenMP 教程。关于调试并行程序的自动化工具,我们鼓励读者看看 Valgrind 中的 Helgrind 和 DRD 工具。
This chapter is meant to give a taste of concurrency topics with threads; it is by no means exhaustive. To learn more about programming with POSIX threads and OpenMP, check out the excellent tutorials on Pthreads and OpenMP by Blaise Barney from Lawrence Livermore National Labs. For automated tools for debugging parallel programs, readers are encouraged to check out the Helgrind and DRD Valgrind tools.
译注
Valgrind 不能在 Apple Silicon Mac 上运行,但 Clang 自带的 ThreadSanitizer 可以直接使用。译者在 Mac 上写了一个两个线程不加锁地递增 counts[i % 10] 的程序,用 clang -g -O1 -fsanitize=thread 编译后运行,输出了「WARNING: ThreadSanitizer: data race」,并指出了发生竞争的源代码行。
在本书的最后一章中,我们会从较高的层次概述其他常见的并行体系结构以及如何为它们编程。继续阅读以了解更多。
In the final chapter of the book, we give a high-level overview of other common parallel architectures and how to program them. Read on to learn more.