<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Concurrency on Cheat Sheet</title><link>/tags/concurrency/</link><description>Recent content in Concurrency on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/concurrency/index.xml" rel="self" type="application/rss+xml"/><item><title>CSP (Communicating Sequential Processes) and Channels</title><link>/programming/go/concurrency/csp/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/go/concurrency/csp/</guid><description>&lt;h1 id="csp-communicating-sequential-processes-and-channels"&gt;CSP (Communicating Sequential Processes) and Channels&lt;a class="anchor" href="#csp-communicating-sequential-processes-and-channels"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;One of the most transformative paradigms in concurrent programming originated in 1978 with C.A.R. Hoare&amp;rsquo;s landmark paper, &lt;strong&gt;&amp;ldquo;Communicating Sequential Processes (CSP)&amp;rdquo;&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Go adopted CSP theory as a first-class citizen in the language design, shaping today&amp;rsquo;s &lt;strong&gt;Goroutines&lt;/strong&gt; and &lt;strong&gt;Channels&lt;/strong&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-what-is-csp"&gt;1. What Is CSP?&lt;a class="anchor" href="#1-what-is-csp"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In the late 1970s, computer science was intensely focused on structured programming (eliminating &lt;code&gt;goto&lt;/code&gt;) and object-oriented paradigms. Concurrency was rarely treated as a fundamental construct; most concurrency relied on &lt;strong&gt;shared memory with lock synchronization&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Channel</title><link>/programming/go/concurrency/channel/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/go/concurrency/channel/</guid><description>&lt;h1 id="channel"&gt;Channel&lt;a class="anchor" href="#channel"&gt;#&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;While the &lt;code&gt;sync&lt;/code&gt; package operates by &lt;strong&gt;synchronizing by sharing memory&lt;/strong&gt;, channels embody the CSP (Communicating Sequential Processes) philosophy of &lt;strong&gt;sharing memory by communicating&lt;/strong&gt;. Rather than guarding shared memory with locks, goroutines pass data safely through channel conduits to achieve synchronization.&lt;/p&gt;
&lt;h2 id="1-core-properties--unidirectional-channels-directionality"&gt;1. Core Properties &amp;amp; Unidirectional Channels (Directionality)&lt;a class="anchor" href="#1-core-properties--unidirectional-channels-directionality"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Each value written to a channel can be &lt;strong&gt;read only once (Single Read)&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;To enforce design intent and prevent misuse at compile time, Go&amp;rsquo;s type system provides &lt;strong&gt;unidirectional channels&lt;/strong&gt;:&lt;/p&gt;</description></item><item><title>Concurrency &amp; Threading</title><link>/programming/cpp/advanced/concurrency/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/advanced/concurrency/</guid><description>&lt;h1 id="c-concurrency-stdthread-vs-lightweight-fibers-channel--abseil-mutex"&gt;C++ Concurrency: &lt;code&gt;std::thread&lt;/code&gt; vs. Lightweight Fibers, &lt;code&gt;Channel&lt;/code&gt;, &amp;amp; Abseil &lt;code&gt;Mutex&lt;/code&gt;&lt;a class="anchor" href="#c-concurrency-stdthread-vs-lightweight-fibers-channel--abseil-mutex"&gt;#&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;This guide covers the architectural differences and usage patterns between &lt;strong&gt;OS kernel threads (&lt;code&gt;std::thread&lt;/code&gt; / &lt;code&gt;std::jthread&lt;/code&gt;)&lt;/strong&gt; and &lt;strong&gt;user-space lightweight threads (&lt;code&gt;thread::Fiber&lt;/code&gt;)&lt;/strong&gt;, &lt;strong&gt;CSP message passing (&lt;code&gt;thread::Channel&amp;lt;T&amp;gt;&lt;/code&gt; &amp;amp; &lt;code&gt;thread::Select&lt;/code&gt;) and how it directly inspired the Go programming language&lt;/strong&gt;, and synchronization with compile-time thread safety verification using open-source &lt;strong&gt;Abseil (&lt;code&gt;abseil-cpp&lt;/code&gt;)&lt;/strong&gt; primitives (&lt;code&gt;absl::Mutex&lt;/code&gt; and &lt;code&gt;ABSL_GUARDED_BY&lt;/code&gt;).&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-os-kernel-threads-stdthread-vs-user-space-lightweight-threads-fibers"&gt;1. OS Kernel Threads (&lt;code&gt;std::thread&lt;/code&gt;) vs. User-Space Lightweight Threads (Fibers)&lt;a class="anchor" href="#1-os-kernel-threads-stdthread-vs-user-space-lightweight-threads-fibers"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In modern high-performance servers, concurrency models generally fall into two categories: &lt;strong&gt;1:1 OS kernel threads&lt;/strong&gt; and &lt;strong&gt;M:N user-space lightweight threads (Fibers)&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Concurrency Concepts</title><link>/cs/concurrency-and-parallelism/concepts/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/cs/concurrency-and-parallelism/concepts/</guid><description>&lt;h1 id="concurrency-concepts"&gt;Concurrency Concepts&lt;a class="anchor" href="#concurrency-concepts"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;When working with concurrent programming, some of the most fundamental yet frequently conflated concepts are &lt;strong&gt;Atomicity&lt;/strong&gt;, &lt;strong&gt;Critical Section&lt;/strong&gt;, &lt;strong&gt;Data Race&lt;/strong&gt;, &lt;strong&gt;Race Condition&lt;/strong&gt;, and the coordination paradigms for execution flows and data pipelines: &lt;strong&gt;Rendezvous&lt;/strong&gt;, &lt;strong&gt;Fork-Join&lt;/strong&gt;, and &lt;strong&gt;Fan-Out/Fan-In&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Understanding the precise definitions and differences among these concepts is crucial for designing correct concurrent systems.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-atomicity"&gt;1. Atomicity&lt;a class="anchor" href="#1-atomicity"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;An operation is considered &lt;strong&gt;atomic&lt;/strong&gt; if, within the context it operates, it is &lt;strong&gt;indivisible&lt;/strong&gt; and &lt;strong&gt;uninterruptible&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Select</title><link>/programming/go/concurrency/select/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/go/concurrency/select/</guid><description>&lt;h1 id="select"&gt;Select&lt;a class="anchor" href="#select"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;If channels are the glue that binds independent goroutines together, the &lt;code&gt;select&lt;/code&gt; statement is &lt;strong&gt;the glue that binds channels together to compose larger program abstractions&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Much like the &lt;code&gt;select(2)&lt;/code&gt; system call in network programming multiplexes multiple file descriptors, Go&amp;rsquo;s &lt;code&gt;select&lt;/code&gt; is a first-class language primitive that coordinates and synchronizes multiple communication operations simultaneously.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-channel-multiplexing-and-blocking"&gt;1. Channel Multiplexing and Blocking&lt;a class="anchor" href="#1-channel-multiplexing-and-blocking"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;select&lt;/code&gt; statement simultaneously evaluates the readiness of channel send and receive operations across multiple cases:&lt;/p&gt;</description></item><item><title>sync</title><link>/programming/go/concurrency/sync/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/go/concurrency/sync/</guid><description>&lt;h1 id="sync"&gt;&lt;code&gt;sync&lt;/code&gt;&lt;a class="anchor" href="#sync"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Go&amp;rsquo;s &lt;code&gt;sync&lt;/code&gt; package is based on &lt;strong&gt;synchronizing by sharing memory&lt;/strong&gt;, providing low-level primitives for memory-access synchronization and critical section protection among goroutines. Unlike channels, which share memory by communicating, &lt;code&gt;sync&lt;/code&gt; deals with traditional locking and signaling primitives.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-syncwaitgroup"&gt;1. &lt;code&gt;sync.WaitGroup&lt;/code&gt;&lt;a class="anchor" href="#1-syncwaitgroup"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Used to establish a &lt;strong&gt;Join Point&lt;/strong&gt; waiting for the completion of concurrent branches in Go&amp;rsquo;s &lt;a href="../../cs/concurrency-and-parallelism/concepts/#51-dimension-1-rendezvous-vs-fork-join-lifecycle--synchronization"&gt;Fork-Join model&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For single goroutine cancellation or waiting, &lt;a href="../../programming/go/std/context/"&gt;context cancellation&lt;/a&gt; is preferred, but &lt;code&gt;WaitGroup&lt;/code&gt; is the standard tool for waiting on multiple concurrent tasks.&lt;/p&gt;</description></item><item><title>Concurrency Patterns</title><link>/programming/go/concurrency/patterns/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/go/concurrency/patterns/</guid><description>&lt;h1 id="go-concurrency-patterns"&gt;Go Concurrency Patterns&lt;a class="anchor" href="#go-concurrency-patterns"&gt;#&lt;/a&gt;&lt;/h1&gt;

&lt;blockquote class='book-hint '&gt;
&lt;p&gt;&amp;ldquo;If a goroutine is responsible for creating a goroutine, it is also responsible for ensuring it can stop the goroutine.&amp;rdquo;&lt;br&gt;
— Katherine Cox-Buday, &lt;em&gt;Concurrency in Go&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Safely combining Go&amp;rsquo;s low-level concurrency primitives (goroutines, channels, &lt;code&gt;select&lt;/code&gt;) in production systems requires proven architectural patterns.&lt;/p&gt;
&lt;p&gt;Most concurrency bugs do not stem from a lack of synchronization primitives, but rather from &lt;strong&gt;goroutine leaks&lt;/strong&gt; caused by inadequate lifecycle management.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-confinement"&gt;1. Confinement&lt;a class="anchor" href="#1-confinement"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The most efficient way to achieve concurrent safety is not to add synchronization primitives, but to &lt;strong&gt;confine access to mutable data to a single concurrent process.&lt;/strong&gt;&lt;/p&gt;</description></item><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><item><title>stdatomic</title><link>/programming/c/stdatomic/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/c/stdatomic/</guid><description>&lt;h1 id="stdatomic"&gt;&lt;code&gt;stdatomic&lt;/code&gt;&lt;a class="anchor" href="#stdatomic"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The C11 standard (&lt;code&gt;&amp;lt;stdatomic.h&amp;gt;&lt;/code&gt;) provides hardware-level atomic memory operations and a formal memory model directly in the language without platform-specific inline assembly.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-why-atomic-operations-are-needed"&gt;1. Why Atomic Operations Are Needed&lt;a class="anchor" href="#1-why-atomic-operations-are-needed"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Concurrent reads and writes to ordinary variables across multiple threads or &lt;code&gt;mmap&lt;/code&gt; shared-memory processes introduce two fundamental hazards:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Lost Update&lt;/strong&gt;: An increment such as &lt;code&gt;total += 100&lt;/code&gt; compiles into three separate machine steps: 1) memory &lt;code&gt;Load&lt;/code&gt;, 2) register &lt;code&gt;Add&lt;/code&gt;, and 3) memory &lt;code&gt;Store&lt;/code&gt;. If two threads read &lt;code&gt;1000&lt;/code&gt; at the same time, both store &lt;code&gt;1100&lt;/code&gt;, losing one increment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Torn Read&lt;/strong&gt;: If one thread reads a 64-bit variable while another thread is halfway through updating it, the reader can observe a corrupted value combining the old upper 32 bits with the new lower 32 bits.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Atomic operations use CPU hardware instructions (eg. x86 &lt;code&gt;LOCK XADD&lt;/code&gt;, ARM &lt;code&gt;LDADD&lt;/code&gt;) to execute the access as a &lt;strong&gt;single indivisible step&lt;/strong&gt;, preventing both Lost Updates and Torn Reads.&lt;/p&gt;</description></item><item><title>Goroutines and the Go Runtime</title><link>/programming/go/concurrency/runtime/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/go/concurrency/runtime/</guid><description>&lt;h1 id="goroutines-and-the-go-runtime"&gt;Goroutines and the Go Runtime&lt;a class="anchor" href="#goroutines-and-the-go-runtime"&gt;#&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;The Go runtime is far more than a standard library—it embeds garbage collection (GC), channel synchronization, memory management, and a &lt;strong&gt;user-space M:N scheduler&lt;/strong&gt; that multiplexes tens of thousands of lightweight goroutines onto a small pool of operating system threads.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-the-work-stealing-algorithm"&gt;1. The Work Stealing Algorithm&lt;a class="anchor" href="#1-the-work-stealing-algorithm"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Go concurrency follows a &lt;strong&gt;Fork-Join model&lt;/strong&gt;, where spawning a goroutine (&lt;code&gt;go func()&lt;/code&gt;) represents a &lt;strong&gt;Fork&lt;/strong&gt;, and waiting on a channel receive (&lt;code&gt;&amp;lt;-ch&lt;/code&gt;) or &lt;code&gt;sync.WaitGroup&lt;/code&gt; (&lt;code&gt;wg.Wait()&lt;/code&gt;) represents a &lt;strong&gt;Join&lt;/strong&gt;.&lt;/p&gt;</description></item></channel></rss>