[{"data":1,"prerenderedAt":117},["ShallowReactive",2],{"term-d\u002Fdata-race":3,"related-d\u002Fdata-race":102},{"id":4,"title":5,"acronym":6,"body":7,"category":83,"description":84,"difficulty":85,"extension":86,"letter":87,"meta":88,"navigation":89,"path":90,"related":91,"seo":96,"sitemap":97,"stem":100,"subcategory":6,"__hash__":101},"terms\u002Fterms\u002Fd\u002Fdata-race.md","Data Race",null,{"type":8,"value":9,"toc":77},"minimark",[10,15,19,23,26,30,66,70,73],[11,12,14],"h2",{"id":13},"eli5-the-vibe-check","ELI5 - The Vibe Check",[16,17,18],"p",{},"A data race is what happens when two threads touch the exact same piece of memory at the exact same time, and at least one of them is writing, with nothing coordinating who goes first. The result is undefined, sometimes it looks fine, sometimes you get half-written garbage, and the truly evil part is it might work perfectly a thousand times in a row and then corrupt data the one time it matters.",[11,20,22],{"id":21},"real-talk","Real Talk",[16,24,25],{},"A data race occurs when two or more threads access the same memory location concurrently, at least one access is a write, and there is no synchronization mechanism establishing an ordering between them. Unlike a race condition, which is a broader logic bug about timing-dependent outcomes, a data race specifically means the program's behavior is undefined by the language's memory model, and tools like ThreadSanitizer exist specifically to catch it.",[11,27,29],{"id":28},"show-me-the-code","Show Me The Code",[31,32,37],"pre",{"className":33,"code":34,"language":35,"meta":36,"style":36},"language-go shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","\u002F\u002F Two goroutines writing the same variable with no synchronization\nvar counter int\ngo func() { counter++ }()\ngo func() { counter++ }() \u002F\u002F data race: run with `go run -race`\n","go","",[38,39,40,48,54,60],"code",{"__ignoreMap":36},[41,42,45],"span",{"class":43,"line":44},"line",1,[41,46,47],{},"\u002F\u002F Two goroutines writing the same variable with no synchronization\n",[41,49,51],{"class":43,"line":50},2,[41,52,53],{},"var counter int\n",[41,55,57],{"class":43,"line":56},3,[41,58,59],{},"go func() { counter++ }()\n",[41,61,63],{"class":43,"line":62},4,[41,64,65],{},"go func() { counter++ }() \u002F\u002F data race: run with `go run -race`\n",[11,67,69],{"id":68},"when-youll-hear-this","When You'll Hear This",[16,71,72],{},"\"ThreadSanitizer flagged a data race on that counter, it's not protected by anything.\" \u002F \"It's not a data race exactly, it's a race condition, the logic is wrong regardless of locking.\"",[74,75,76],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":36,"searchDepth":50,"depth":50,"links":78},[79,80,81,82],{"id":13,"depth":50,"text":14},{"id":21,"depth":50,"text":22},{"id":28,"depth":50,"text":29},{"id":68,"depth":50,"text":69},"backend","A data race is what happens when two threads touch the exact same piece of memory at the exact same time, and at least one of them is writing, with nothing...","intermediate","md","d",{},true,"\u002Fterms\u002Fd\u002Fdata-race",[92,93,94,95],"Mutex","Memory Ordering","Race Condition","Lock-Free",{"title":5,"description":84},{"changefreq":98,"priority":99},"weekly",0.7,"terms\u002Fd\u002Fdata-race","ZZj06I838RbZFGJAbjLw_cNUylCydUjFf_JP63o_f-8",[103,107,110,113],{"title":95,"path":104,"acronym":6,"category":83,"difficulty":105,"description":106},"\u002Fterms\u002Fl\u002Flock-free","advanced","Lock-free code gets multiple threads to safely share data without ever making anyone stand in line waiting for a lock.",{"title":93,"path":108,"acronym":6,"category":83,"difficulty":105,"description":109},"\u002Fterms\u002Fm\u002Fmemory-ordering","Memory ordering is the unsettling fact that the order you wrote your instructions and the order the CPU and compiler actually carry them out are not the sa...",{"title":92,"path":111,"acronym":6,"category":83,"difficulty":85,"description":112},"\u002Fterms\u002Fm\u002Fmutex","A mutex is a single key to a single bathroom.",{"title":94,"path":114,"acronym":6,"category":115,"difficulty":85,"description":116},"\u002Fterms\u002Fr\u002Frace-condition","general","A race condition is when two parts of your code are racing to do something at the same time and the winner isn't guaranteed — leading to unexpected, hard-t...",1792528077092]