TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Go's memory model [pdf]

4 点作者 henridf超过 6 年前

1 comment

karmakaze超过 6 年前
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>