Notes
Search…
The Go Memory Model
https://golang.org/ref/mem

Introduction

The Go memory model specifies the conditions under which reads of a variable in one goroutine can be guaranteed to observe values produced by writes to the same variable in a different goroutine.

Advice

Programs that modify data being simultaneously accessed by multiple goroutines must serialize such access.
To serialize access, protect the data with channel operations or other synchronization primitives such as those in the sync and sync/atomic packages.

Happens Before

Within a single goroutine, reads and writes must behave as if they executed in the order specified by the program. That is, compilers and processors may reorder the reads and writes executed within a single goroutine only when the reordering does not change the behavior within that goroutine as defined by the language specification.
Within a single goroutine, reads and writes must behave as if they executed in the order specified by the program. That is, compilers and processors may reorder the reads and writes executed within a single goroutine only when the reordering does not change the behavior within that goroutine as defined by the language specification.
To specify the requirements of reads and writes, we define happens before, a partial order on the execution of memory operations in a Go program. If event e1 happens before event e2, then we say that e2 happens after e1. Also, if e1 does not happen before e2 and does not happen after e2, then we say that e1 and e2 happen concurrently.
  • Within a single goroutine, the happens-before order is the order expressed by the program.
A read r of a variable v is allowed to observe a write w to v if both of the following hold:
  1. 1.
    r does not happen before w.
  2. 2.
    There is no other write w' to v that happens after w but before r.
To guarantee that a read r of a variable v observes a particular write w to v, ensure that w is the only write r is allowed to observe. That is, r is guaranteed to observe w if both of the following hold:
  1. 1.
    w happens before r.
  2. 2.
    Any other write to the shared variable v either happens before w or after r.
Within a single goroutine, there is no concurrency, so the two definitions are equivalent: a read r observes the value written by the most recent write w to v. When multiple goroutines access a shared variable v, they must use synchronization events to establish happens-before conditions that ensure reads observe the desired writes.

Synchronization

Initialization

  • If a package p imports package q, the completion of q's init functions happens before the start of any of p's.
  • The start of the function main.main happens after all init functions have finished.

Goroutine creation

  • The go statement that starts a new goroutine happens before the goroutine's execution begins.

Goroutine destruction

  • The exit of a goroutine is not guaranteed to happen before any event in the program.
If the effects of a goroutine must be observed by another goroutine, use a synchronization mechanism such as a lock or channel communication to establish a relative ordering.

Channel communication

  • A send on a channel happens before the corresponding receive from that channel completes.
  • The closing of a channel happens before a receive that returns a zero value because the channel is closed.
  • A receive from an unbuffered channel happens before the send on that channel completes.
  • The kth receive on a channel with capacity C happens before the k+Cth send from that channel completes.
Rule 4 generalizes the rule 3 to buffered channels.

Locks

  • For any sync.Mutex or sync.RWMutex variable l and n < m, call n of l.Unlock() happens before call m of l.Lock() returns.
  • For any call to l.RLock on a sync.RWMutex variable l, there is an n such that the l.RLock happens (returns) after call n to l.Unlock and the matching l.RUnlock happens before call n+1 to l.Lock.

Once

  • A single call of f() from once.Do(f) happens (returns) before any call of once.Do(f) returns.

Incorrect synchronization

1
var a, b int
2
3
func f() {
4
a = 1
5
b = 2
6
}
7
8
func g() {
9
print(b)
10
print(a)
11
}
12
13
func main() {
14
go f()
15
g()
16
}
17
18
// may print 2 and then 0
Copied!
1
var a string
2
var done bool
3
4
func setup() {
5
a = "hello, world"
6
done = true
7
}
8
9
func doprint() {
10
if !done {
11
once.Do(setup)
12
}
13
print(a)
14
}
15
16
func twoprint() {
17
go doprint()
18
go doprint()
19
}
20
21
// may print empty string
Copied!
1
var a string
2
var done bool
3
4
func setup() {
5
a = "hello, world"
6
done = true
7
}
8
9
func main() {
10
go setup()
11
for !done {
12
}
13
print(a)
14
}
15
16
// may print empty string
17
// main() may never finish
Copied!
1
type T struct {
2
msg string
3
}
4
5
var g *T
6
7
func setup() {
8
t := new(T)
9
t.msg = "hello, world"
10
g = t
11
}
12
13
func main() {
14
go setup()
15
for g == nil {
16
}
17
print(g.msg)
18
}
19
20
// main() may never exits its loop
21
// may print empty string
Copied!
Last modified 5mo ago