Summary Dialogue on Concurrency

Part II πŸ’¬ Dialogue OSTEP pp. 413–416 Β· ~5 min read

The concurrency arc closes as the others did β€” with the professor, the student, and an aching head.

Professor:So, does your head hurt now?
Student:(taking two Motrin tablets) Well, some. It’s hard to think about all the ways threads can interleave.
Professor:Indeed it is. I’m always amazed that when concurrent execution is involved, just a few lines of code can become nearly impossible to understand.
Student:It’s kind of embarrassing, as a Computer Scientist, not to be able to make sense of five lines of code.
Professor:Oh, don’t feel too badly. If you look through the first papers on concurrent algorithms, they are sometimes wrong! And the authors often professors!
Student:(gasps) Professors can be… umm… wrong?
Professor:Yes, it’s true. Though don’t tell anybody β€” it’s one of our trade secrets. But if concurrent code is so hard to get right, how are we supposed to write it correctly? That’s the real question. So β€” what’s in your mental model now?

The student’s answer, itemized (click each row β€” it notes where the belief was earned):

The student's mental model of concurrency β€” eight load-bearing beliefs, and where each was earned
the belief
1Threads share everything β€” and that's the problem
2A lock is built, not given
3Add locks carefully β€” one big lock first
4Condition variables let a thread sleep until state changes
5One primitive can be both lock and CV
6Concurrency bugs come in patterns
7Threads aren't the only way
8The best concurrency is the concurrency you avoid
Dotted-underlined cells have explanations β€” click one.
Professor:A fine model! Now let me give you the three rules I live by for writing correct concurrent code.

The Professor’s Three Rules of Concurrency

1. Keep it simple. Avoid complex thread interactions; use well-known, tried-and-true paradigms β€” simple locking , a producer/consumer queue.
2. Use concurrency only when absolutely needed. Avoid it if you can β€” there is nothing worse than premature optimization.
3. If you truly need parallelism, seek simplified forms. MapReduce lets you write parallel data analysis with no locks, condition variables, or other nasty things at all.

Student:Like simple locking, and maybe a producer-consumer queue? And… why add threads if you don’t need them?
Professor:Exactly! Those are common paradigms you can now build. And for real parallelism, look at MapReduce β€” parallelism without any of the horrific complexities we’ve talked about.
Student:Map-Reduce, huh? Sounds interesting β€” I’ll have to read more about it on my own.
Professor:Good! You should. What we learn together is only the barest introduction. Read, read, and read some more! Then write code, and write some more. As Gladwell says in Outliers, you need roughly 10,000 hours to become a real expert β€” you can’t do that inside class time. (Want a fun place to practice deadlock? Try the gamified Deadlock Empire.)
Student:Wow, I’m not sure if that’s depressing or uplifting. But I’ll assume the latter, and get to work! Time to write some more concurrent code…

Next: Part III β€” Persistence

We virtualized one CPU into many, one memory into many address spaces, and made many threads cooperate without stepping on each other. The final piece: making data survive β€” I/O devices, spinning disks and SSDs, RAID, file systems, journaling, and the long fight to keep bits safe across crashes and time. The third easy piece begins.

Check yourself: the concurrency wrap-up

1.What is the Professor's FIRST rule for writing correct concurrent code?

2.What is the Professor's second rule?

3.The Professor's third rule points to MapReduce as an example of what?

4.Recapping the Part: what fundamentally causes a race condition (chapter 26)?

5.Recap: why does the producer/consumer solution need TWO condition variables (chapter 30)?

5 questions