TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Go's memory model [pdf]

4 pointsby henridfover 6 years ago

1 comment

karmakazeover 6 years ago
I was hoping that there were answers here. Instead I got:<p><pre><code> Go Memory Model Two purposes: ● Make guarantees for programmers. ● Allow compilers&#x2F;hardware to make certain changes to programs. Ideally, perfectly balanced. In practice, more conservative: might do neither. Explicit concern: Leave room for future refinement, refraining from: ● making debatable guarantees to programmers ● allowing compilers&#x2F;hardware to make debatable changes to programs </code></pre> and specifically I was curious about sync&#x2F;atomic and only got a proposal (which ironically says &#x27;be like Java&#x27;):<p><pre><code> Package sync&#x2F;atomic is conspicuously missing. Proposal: match Java volatile and C++ memory_order_seq_cst. ● An atomic write happens before an atomic read that observes the write. ● The outcome of atomic operations must be consistent with a total order over all atomic operations in the program. </code></pre> TL;DR<p><pre><code> Go Memory Model Semantics based on happens-before: ● If p imports q, q’s init happens before p’s. ● Package main’s init happens before main.main ● The go statement happens before the created goroutine’s execution ● A send (or close) on a channel happens before the receive ● Unlock happens before subsequent Lock A read r is allowed to observe a write w to location v if: ● r does not happen before w ● There is no write w’ to v such that w &lt; w’ &lt; r. [&lt; is happens-before] A read r is therefore guaranteed to observe w if: ● w happens before r (w &lt; r) ● For any other write w’ to v, w’ &lt; w or r &lt; w’ Intent: like DRF-SC without allowing compilers to ruin programmers’ lives. </code></pre> <i>Note: DRF-SC Data Race Free-Sequentially Consistent</i>