Previous | Next --- Slide 31 of 63
Back to Lecture Thumbnails
pslui88

As reviewed in lecture, the increment (x++) instruction in C actually expands to 3 assembly instructions. Thus, if 2 concurrent threads are executing x++, their respective assembly instructions can be interleaved in a way where both threads start by seeing that diff contains 1, so both end up updating diff to 1, instead of what we probably actually want, which is for one thread to atomically update diff to 1, then the other to update it to 2. This is why we need mutual exclusion, implemented using locks.

Please log in to leave a comment.