Race condition

경쟁 상태

The final value changes with the interleaving of concurrent reads and writes.

···
html
<div class="v"><header><b>LOST UPDATE</b><span id="state"></span></header><main><div class="workers" id="workers"></div><div class="total" id="total"></div></main><footer>two increments can yield one</footer></div>
css
.v{width:min(92vw,660px);height:min(86vh,310px);box-sizing:border-box;padding:clamp(9px,2.5vmin,18px);border:1px solid var(--line);border-radius:14px;background:var(--surface);display:flex;flex-direction:column;gap:7px;color:var(--fg);font:500 clamp(15px,3.2vmin,17px)/1.25 var(--font-sans, sans-serif)}.v header,.v footer{display:flex;justify-content:space-between;align-items:center;gap:8px;white-space:nowrap}.v header b{color:var(--accent);font-size:.9em}.v header span,.v footer{color:var(--muted);font-size:.82em}.v main{flex:1;min-height:0;position:relative;overflow:hidden}.v .mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.v .active{background:var(--accent)!important;color:var(--bg)!important;border-color:var(--accent)!important}.v .muted{opacity:.28} .workers{height:67%;display:flex;justify-content:space-around;gap:8px}.worker{width:44%;border:1px solid var(--line);border-radius:7px;padding:8px;text-align:center;font:600 .82em ui-monospace,monospace}.worker b{display:block;margin-top:10px;color:var(--accent)}.total{text-align:center;color:var(--accent);font:700 .9em ui-monospace,monospace}
js
let n=0;const steps=[["read 0","read 0","counter = 0"],["write 1","read 0","counter = 1"],["done","write 1","counter = 1"],["read 1","write 2","counter = 2"]];function draw(){const s=steps[n];document.getElementById("workers").innerHTML="<div class=worker>WORKER A<b>"+s[0]+"</b></div><div class=worker>WORKER B<b>"+s[1]+"</b></div>";document.getElementById("total").textContent=s[2];document.getElementById("state").textContent=n===3?"serialized":"interleaved";n=(n+1)%4}draw();setInterval(draw,1050)

If two jobs read the same value, add one, and write it back, one update can be lost. Read-modify-write is several steps rather than one indivisible action.

The bug depends on scheduling and I/O timing. The demo compares both jobs reading zero with one job reading after the other writes.

When to use

Look for it when several jobs modify shared counters, inventory, or request state.

Open as page ↗