Textbook review: Is Parallel Programming Hard, And, If So, What Can You Do About It?
Textbook review: Is Parallel Programming Hard, And, If So, What Can You Do About It?
教科书评测:《并行编程难吗?如果是,你该怎么办?》
Here are my thoughts on the free online textbook Is Parallel Programming Hard, And, If So, What Can You Do About It? by Paul E. McKenney, author of the Linux kernel’s RCU synchronization mechanism. I read a lot of this textbook, got my fill, and probably won’t read more of it in the near future so wanted to write this review while it is all still fresh. 以下是我对免费在线教科书《并行编程难吗?如果是,你该怎么办?》(Is Parallel Programming Hard, And, If So, What Can You Do About It?)的看法。该书作者是 Linux 内核 RCU 同步机制的开发者 Paul E. McKenney。我阅读了这本书的大部分内容,收获颇丰,近期可能不会再深入研读了,因此想趁着记忆犹新写下这篇评测。
Set & Setting
背景与环境
Here I’ll talk about the mindset/life phase and physical setting I was in when I started reading this textbook. This might be like the tedious personal flavor preamble they have on recipe websites so skip ahead if that does not interest you. 在这里,我将谈谈我开始阅读这本书时的心态、人生阶段以及所处的物理环境。这可能就像食谱网站上那些冗长乏味的个人背景介绍,如果你不感兴趣,可以直接跳过。
My professional life had revolved around TLA⁺ & distributed systems for the past decade, and I was thinking it was time for a change. A transitional and emotionally tumultuous period! In 2022 I had tried (and failed) to move to Lean, hoping to acquire an unbelievably niche & nonexistent job as the guy who formalizes researchers’ quantum information processing results for them. I burned out on that, which given recent advances in automated theorem proving might have been my temporarily-prescient nervous system dodging me a bullet. 过去十年,我的职业生涯一直围绕着 TLA⁺ 和分布式系统,我当时觉得是时候做出改变了。那是一段充满变动且情绪波动的时期!2022 年,我曾尝试(但失败了)转向 Lean 语言,希望能找到一份极其小众且几乎不存在的工作——专门为研究人员形式化验证量子信息处理结果。我因此精疲力竭,考虑到自动定理证明领域的最新进展,这或许是我当时具有先见之明的神经系统在帮我避开一个坑。
Thus was the history & context in which I attended the 2026 Software Should Work conference in Columbia, Missouri. The conference had a lot of good talks, but I especially enjoyed the one on Fil-C by Filip Pizlo: I also got to talk to Fil a fair bit, about interpreters and then about concurrency. I fancied myself pretty knowledgeable about concurrency from TLA⁺ & distributed systems, but Fil told me about the difficulty of writing a concurrent lock-free garbage collector and I realized I actually knew very little about concurrency (feeling that you know very little is the mark of a good conference). 这就是我参加 2026 年在密苏里州哥伦比亚市举办的“软件应该工作”(Software Should Work)大会的历史背景。会议有很多精彩的演讲,但我特别喜欢 Filip Pizlo 关于 Fil-C 的那场:我还有幸与 Fil 聊了很久,从解释器聊到了并发。我曾自诩通过 TLA⁺ 和分布式系统对并发有相当深入的了解,但 Fil 向我讲述了编写并发无锁垃圾回收器的难度,我才意识到自己对并发知之甚少(感到自己知之甚少,正是优秀会议的标志)。
Fil also mentioned TLA⁺ might not be useful (or at least ergonomic) for reasoning about events which happen literally concurrently (an actual possibility with a multicore CPU!) and the importance of analyzing concurrent algorithms for linearizability, a concept I sort of understood in the distributed systems sense. All of this seemed very alluring, so I looked around for a textbook to read about concurrency that focused more on lock-free aspects as opposed to mutex-based or message-passing patterns. Fil 还提到,TLA⁺ 在推理“真正并发”(多核 CPU 下的真实可能性!)发生的事件时可能并不好用(或者至少不够顺手),并强调了分析并发算法线性一致性(Linearizability)的重要性——这个概念我在分布式系统领域略知一二。这一切听起来非常吸引人,于是我开始寻找一本专注于无锁编程,而非基于互斥锁或消息传递模式的并发教科书。
Is Parallel Programming Hard, And, If So, What Can You Do About It? seemed to fit the bill, focusing as it does on general concurrent programming & CPU cache effects instead of more specific textbooks about how to write lock-free datastructures. It also had a few (2023, 2021, 2020, 2015, 2014, 2011) moderately interesting HN threads. I don’t think it’s useful spending time in analysis paralysis trying to find the exact “right” textbook (this is really just a clever way to procrastinate), so it seemed good enough. 《并行编程难吗?如果是,你该怎么办?》似乎正合我意,它专注于通用并发编程和 CPU 缓存效应,而不是那些专门教你如何编写无锁数据结构的教材。它在 Hacker News 上也有过几次(2023、2021、2020、2015、2014、2011 年)相当有趣的讨论。我认为没必要陷入“分析瘫痪”去寻找那本绝对“正确”的教科书(那其实只是拖延症的巧妙借口),所以这本书对我来说已经足够好了。
The physical setting in which I read this textbook was a 1.5 week vacation to visit my family in a quiet, wooded part of Canada. 2026 also turned out to be a particularly horrific mosquito season. Thus I spent much of the time sitting in a cool screened-in patio, diligently watched over by hundreds of guards ensuring I did not leave my post: Satellites & cell towers have made distracting internet connectivity annoyingly good even in the more remote parts of the country, but otherwise this was an optimal textbook reading location. 我阅读这本书的物理环境是在加拿大一个安静的林区,当时我正在那里休假一周半探望家人。2026 年恰逢蚊灾极其严重的一年。因此,我大部分时间都坐在凉爽的纱窗露台上,在数百名“守卫”的严密监视下,确保我不会擅离职守:卫星和基站让即使在偏远地区也能享受到令人分心的极佳网络连接,但除此之外,这确实是一个阅读教科书的绝佳地点。
The Textbook Format
教科书格式
Some quick notes on the actual structure of the textbook; it is available in no fewer than three separate formats, all PDF: 关于这本书的实际结构,有几点说明;它提供了不少于三种格式,全部为 PDF:
- A dual-column format, like a scientific paper
- 双栏格式,像科学论文一样
- A single-column format with large margins
- 大页边距的单栏格式
- A single-column format with no margins
- 无页边距的单栏格式
The last one is perfect for reading on my Pine64 PineNote. The textbook contains a huge number of internal links. Some of these links are used in quick knowledge-check question boxes, where clicking the link takes you to the question’s answer. Other links are used whenever a figure or section is mentioned, or for copious footnotes & citations. 最后一种格式非常适合在我的 Pine64 PineNote 上阅读。书中包含大量的内部链接。其中一些链接用于快速知识检查框,点击即可跳转到答案。其他链接则用于引用图表或章节,以及大量的脚注和引文。
Unfortunately the latter are very annoying and should probably be reduced by at least 80%. If your e-reader lacks physical page-turn buttons, then your experience of reading the book will consist of constantly accidentally pressing one of these links when you meant to turn the page and thus being sent who-knows-where. E-books are disorienting enough to navigate without this, and it pretty much meant it was impossible to quickly flip back & forth between two sections using repeated page-turn taps. 遗憾的是,后者非常烦人,可能应该减少至少 80%。如果你的电子阅读器没有物理翻页键,那么你的阅读体验将变成:本想翻页却不断误触这些链接,导致被传送到不知名的地方。电子书本身导航就够让人迷失方向了,这几乎意味着你无法通过重复点击翻页来在两个章节之间快速切换。
For times where I did want to click a link, some places had two links right next to each other; touch screens lack the precision to reliably click one link instead of the other. The solution of simply disabling all links presents itself, but then you lose access to the quite nice knowledge-check questions. So I just suffered through it. 即使在我想点击链接的时候,有些地方两个链接靠得太近;触摸屏缺乏足够的精度来可靠地点击其中一个。解决办法似乎是直接禁用所有链接,但这样你就无法使用那些非常棒的知识检查题了。所以我只能忍受。
The Textbook Content - Introductory Chapters
教科书内容 - 导论章节
The textbook was more or less the perfect presentation of material for my level. The book starts with nice light introduction & motivation chapters before heading off to the races in chapter 3, Hardware and its Habits. Here we learn about how modern CPUs work at a high level - what makes them fast, and what makes them slow. Complete with a bunch of humorous illustrations! 这本书的内容呈现方式对我目前的水平来说堪称完美。书的开头是轻松的导论和动机章节,随后在第三章“硬件及其习惯”(Hardware and its Habits)中进入正题。在这里,我们从高层视角了解了现代 CPU 的工作原理——是什么让它们变快,又是什么让它们变慢。书中还配有大量幽默的插图!
Section 3.2.1 - Hardware System Architecture is where it really got interesting for me, as we are walked through a simplified account of a CPU core writing to a memory address that does not exist in its cache. Here is one missed opportunity: I would have really benefited from a basic explanation of the MESI protocol, possessing essentially no intuition about how CPU caches mediate concurrent reads & writes. 第 3.2.1 节“硬件系统架构”是我觉得最有趣的部分,书中通过一个简化的案例,讲解了 CPU 核心如何向其缓存中不存在的内存地址写入数据。这里有一个遗憾:如果能对 MESI 协议做一个基础解释,我会受益匪浅,因为我当时对 CPU 缓存如何协调并发读写几乎没有任何直觉。
I found out about MESI while searching online to better understand this section; MESI is only mentioned in the appendix of this book. But my understanding of the rest of the book was greatly improved by knowing about it. Learning about MESI also taught me that multiple CPU cores cannot write to the same data location literally concurrently! An x86 CPU doesn’t actually write directly to memory, 我在网上搜索以更好地理解这一节时才发现了 MESI;MESI 在书中仅在附录中提到。但了解它之后,我对全书其余部分的理解大有裨益。学习 MESI 还让我明白,多个 CPU 核心无法真正“同时”写入同一个数据位置!x86 CPU 实际上并不会直接写入内存,