Free Synchronization lessonsSynchronization · Stage 0 · The problem: sharing data · Step 1
Race conditions
See how two threads incrementing one counter can lose an update, and why it only happens sometimes. About 13 minutes.
Two threads each run counter++ once on a shared counter starting at 0. Which final values are possible?
- A1 or 2, depending on how the instructions interleave
- BAlways 2
- CAny number, since it's random
Show the answer
1 or 2, depending on how the instructions interleave. If both load before either stores, one update is lost and the result is 1.
4 questions on this idea
Short questions that apply the idea to new cases: trace it, work out a number, put steps in order. A wrong answer explains itself.
Answer them in your browserFree account, no card. Your progress is saved, and the idea comes back in a quick review later.
Sign up free to solve