<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Mutex on Cheat Sheet</title><link>/tags/mutex/</link><description>Recent content in Mutex on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/mutex/index.xml" rel="self" type="application/rss+xml"/><item><title>Mutex vs Atomic</title><link>/cs/concurrency-and-parallelism/mutex-vs-atomic/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/cs/concurrency-and-parallelism/mutex-vs-atomic/</guid><description>&lt;h1 id="mutex-vs-atomic"&gt;Mutex vs Atomic&lt;a class="anchor" href="#mutex-vs-atomic"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The two foundational primitives for eliminating Data Races in shared-memory concurrency are &lt;strong&gt;Mutexes&lt;/strong&gt; and &lt;strong&gt;Atomic operations&lt;/strong&gt;. They differ fundamentally in the scope of state they can protect and how they handle contention.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-core-comparison"&gt;1. Core Comparison&lt;a class="anchor" href="#1-core-comparison"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Dimension&lt;/th&gt;
 &lt;th style="text-align: left"&gt;&lt;strong&gt;Mutex&lt;/strong&gt;&lt;/th&gt;
 &lt;th style="text-align: left"&gt;&lt;strong&gt;Atomic&lt;/strong&gt;&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Protected Scope&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Multiple variables, multi-statement blocks, entire composite data structures&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;A single scalar or pointer variable, a single operation&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Contention Behavior&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;OS kernel or runtime scheduler &lt;strong&gt;blocks&lt;/strong&gt; the contending thread and performs a Context Switch&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Never sleeps; completes directly via &lt;strong&gt;a single CPU hardware instruction&lt;/strong&gt; (eg. &lt;code&gt;LOCK XADD&lt;/code&gt;, &lt;strong&gt;Lock-free&lt;/strong&gt;)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Typical Cost&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Microseconds under contention (kernel transition and scheduling overhead)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Nanoseconds (hardware cache-coherency protocol overhead)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Typical Use Cases&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Linked-list/map mutations, multi-field invariants&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Single counters, state flags, reference counting&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id="2-shared-rule-every-access-path-must-synchronize"&gt;2. Shared Rule: Every Access Path Must Synchronize&lt;a class="anchor" href="#2-shared-rule-every-access-path-must-synchronize"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A common misconception is that only Writers modifying a variable need Mutexes or Atomic operations while Readers can read the variable plainly without synchronization.&lt;/p&gt;</description></item></channel></rss>