Labs / Computer Science
Race Condition Lab
Two threads each try to add 1 to the same counter, over and over. But “add 1” is really three steps — read, increment, write — and when those steps interleave, an update can quietly disappear. Turn on the mutex to fix it, or switch to Manual and click a thread to schedule the steps yourself.
expected 16counter 0lost 0
What to try
- With no lock, why does the counter almost never reach the expected total, even though every increment “ran”?
- Switch to Manual and interleave the steps by hand: can you make both threads read the same value and then clobber each other on write?
- Turn on the mutex lock. Where do the lost updates go — and what is Thread B doing while it says “waiting for lock”?
- Set one thread very fast and the other very slow. Do collisions happen more often or less often than when both run at equal speed?
- If two threads can lose an update, how many might vanish with ten threads — and is a single shared lock still enough?