Lesson 13: Concurrency & Parallelism


Imagine this setup code:

zero: int = const 0;
one: int = const 1;
a: ptr<int> = alloc one;
b: ptr<int> = alloc one;
store a zero;
store b zero;

that stores 0 in two pointers, a and b. Then imagine these two threads running concurrently:

# thread 1
store a one;
bv: int = load b;

# thread 2
store b one;
av: int = load a;

What are the possible values of av and bv at the end? If you said the set {(0, 1), (1, 0), (1, 1)}, then you're implicitly imagining that parallel execution looks like an interleaving of the instructions from parallel threads. That would be nice! It's a memory model called sequential consistency. (For more on memory models, I strongly recommend a primer by Sorin, Hill, and Wood.)


There are no tasks for this lesson other than pondering the deep weirdness of programming with shared-memory multithreading. Good luck on your course project!