Questions / Quant development, Concurrency & coordination
Lose an increment
Model an increment as separate read and write steps. Two workers both read x=0, each computes 1, and each writes 1. What is the final stored value in this interleaving?
Answer it in the app It's free. You get hints, a second try if you miss, and a follow-up question.
Show the answer and worked solution
Answer: 1 value
- Both workers read the same starting value 0 before either write is applied.
- Each independently computes 0+1, so the two writes both store 1.
- The second write replaces 1 with 1; the final stored value is 1. This is an abstract interleaving, not a claim that a C++ data race has defined behavior.
Interview shortcut. Read-modify-write without atomicity loses updates.
Takeaway. Protect the read-modify-write operation as a unit.
In the app this has a follow-up where one thing about the setup changes. Try the follow-up.