[{"data":1,"prerenderedAt":111},["ShallowReactive",2],{"term-m\u002Fmutex":3,"related-m\u002Fmutex":96},{"id":4,"title":5,"acronym":6,"body":7,"category":77,"description":78,"difficulty":79,"extension":80,"letter":81,"meta":82,"navigation":83,"path":84,"related":85,"seo":90,"sitemap":91,"stem":94,"subcategory":6,"__hash__":95},"terms\u002Fterms\u002Fm\u002Fmutex.md","Mutex",null,{"type":8,"value":9,"toc":71},"minimark",[10,15,19,23,26,30,60,64,67],[11,12,14],"h2",{"id":13},"eli5-the-vibe-check","ELI5 - The Vibe Check",[16,17,18],"p",{},"A mutex is a single key to a single bathroom. Only one thread can hold the key at a time, so only one thread can be inside the critical section of code touching some shared piece of data. Everyone else waits in line outside the door until the key gets handed back.",[11,20,22],{"id":21},"real-talk","Real Talk",[16,24,25],{},"A mutex (mutual exclusion lock) is a synchronization primitive that allows only one thread to hold it at a time, used to protect a critical section of code that accesses shared, mutable state. Threads that attempt to acquire an already-held mutex block until it is released, which prevents data races but can introduce contention and, if used carelessly across multiple locks, deadlock.",[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","mu.Lock()\ndefer mu.Unlock()\ncounter++ \u002F\u002F safe: only one goroutine at a time\n","go","",[38,39,40,48,54],"code",{"__ignoreMap":36},[41,42,45],"span",{"class":43,"line":44},"line",1,[41,46,47],{},"mu.Lock()\n",[41,49,51],{"class":43,"line":50},2,[41,52,53],{},"defer mu.Unlock()\n",[41,55,57],{"class":43,"line":56},3,[41,58,59],{},"counter++ \u002F\u002F safe: only one goroutine at a time\n",[11,61,63],{"id":62},"when-youll-hear-this","When You'll Hear This",[16,65,66],{},"\"Wrap that shared map access in a mutex before two goroutines corrupt it.\" \u002F \"We're seeing lock contention because everything funnels through one giant mutex.\"",[68,69,70],"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":72},[73,74,75,76],{"id":13,"depth":50,"text":14},{"id":21,"depth":50,"text":22},{"id":28,"depth":50,"text":29},{"id":62,"depth":50,"text":63},"backend","A mutex is a single key to a single bathroom.","intermediate","md","m",{},true,"\u002Fterms\u002Fm\u002Fmutex",[86,87,88,89],"Data Race","Lock Contention","Deadlock","Lock-Free",{"title":5,"description":78},{"changefreq":92,"priority":93},"weekly",0.7,"terms\u002Fm\u002Fmutex","-8EhCtp_oM64-uKEZsRn4Ht7gI5lbQ0xYMfD-TwXrmw",[97,100,104,107],{"title":86,"path":98,"acronym":6,"category":77,"difficulty":79,"description":99},"\u002Fterms\u002Fd\u002Fdata-race","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...",{"title":88,"path":101,"acronym":6,"category":102,"difficulty":79,"description":103},"\u002Fterms\u002Fd\u002Fdeadlock","general","A deadlock is when two things are each waiting for the other to go first, and neither ever does. Thread A holds Lock 1 and wants Lock 2.",{"title":87,"path":105,"acronym":6,"category":77,"difficulty":79,"description":106},"\u002Fterms\u002Fl\u002Flock-contention","Lock contention is what happens when a mutex becomes the most popular door in the building, and instead of doing useful work, half your threads are just st...",{"title":89,"path":108,"acronym":6,"category":77,"difficulty":109,"description":110},"\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.",1792528101874]