Previous | Next --- Slide 30 of 63
Back to Lecture Thumbnails
fizzbuzz

There's an excellent paper by Hans J. Boehm about why your language actually has to be somewhat aware of threads to have any reasonable expectation of consistency, and just saying that you want sequential consistency (where any compilation of a thread that executes equivalently to some sequential execution is valid) is not enough.

Consider if we didn't have locks here. The compiler could decide to change thread 1 into something like ``` int x = 0;

spawn_thread(foo, &x, &my_lock);

int y = x; x = 0xdeadbeee; x = y; x++; ```

and just make the obvious compilation for thread 2.

Then thread 2 could preempt thread after thread 1 writes 0xdeadbeee, and run to completion, printing 0xdeadbeef. While the idea that any compiler would do this sort of thing is comical, it shows that without synchronization you can get practically any result. This is why you have to be extremely careful with lock-free data structures an algorithms.

ufxela

@fizzbuzz cool paper! also do you by chance love structs and bit fields...?

Please log in to leave a comment.