Go memory model — happens-before, channels, and sync primitives

Contents

  1. 1. Memory model
    1. 1.1. Happens before
    2. 1.2. Synchronization
      1. 1.2.1. Package initialization
      2. 1.2.2. Goroutines
      3. 1.2.3. Channels
      4. 1.2.4. sync package

Notes on how the Go memory model orders reads and writes.

Memory model

Happens before

The model defines when reads and writes are ordered. Without a defined order, you cannot rely on result ordering.

For a write w to V to be observed by a read r:

  1. r does not happen before w, and
  2. no other write w' happens after w and before r.

For w to be the only value r can see (no concurrent conflicting writes):

  1. w happens before r, and
  2. every other write to V happens after r or before w.

Variable initialization counts as a write in the memory model.
Reads and writes larger than one machine word may appear as several word-sized operations without a single total order (word size = width of a native integer operation on the CPU).

Synchronization

Package initialization

  • If package p imports package q, q‘s init happens before p‘s init.
  • main.main happens after all package init functions.

Goroutines

  • Starting a goroutine with go happens before that goroutine runs.
  • Goroutine exit is not guaranteed to happen before any other operation.

Channels

  • A send on a channel happens before the corresponding receive completes on that channel.
  • Closing a channel happens before a receive that returns the zero value because the channel is closed.
  • On an unbuffered channel, a receive happens before the send completes.
1
2
3
4
5
6
7
8
9
10
11
12
var c = make(chan int)
var a string

func f() {
a = "hello, world"
<-c // happens before c <- 0
}
func main() {
go f()
c <- 0
print(a)
}
  • On a channel with capacity c, the kth receive happens before the (k+c)th send completes.
1
2
3
4
5
6
7
8
9
10
11
12
13
// often used to limit concurrency
var limit = make(chan int, 3)

func main() {
for _, w := range work {
go func(w func()) {
limit <- 1
w()
<-limit
}(w)
}
select{}
}

sync package

  • For sync.Mutex or sync.RWMutex, if n < m, the nth Unlock() happens before the mth Lock().

    There must be an Unlock between two Lock calls; otherwise the second Lock blocks forever.

RWMutex: multiple readers can hold the lock together; readers block writers; writers exclude everyone.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var l sync.Mutex
var a string

func f() {
a = "hello, world"
l.Unlock()
}

func main() {
l.Lock()
go f()
l.Lock() // ensures f runs before print(a)
print(a)
}
  • For sync.RWMutex, the nth Unlock() happens before an RLock() that returns after call n to Unlock, and the matching RUnlock() happens before call n+1 to Lock().

For any RLock on l, there is an n such that RLock returns after the nth Unlock, and the matching RUnlock happens before the (n+1)th Lock.

1
2
3
4
5
// ordering sketch
l.Unlock()
l.RLock()
l.RUnlock()
l.Lock()
  • Within a single once.Do(f) invocation, f returning happens before any other once.Do(f) call returns.

    Concurrent once.Do(f) calls: only the first runs f; the rest block until f returns.

Questions? Leave a comment below or email: newbvirgil@gmail.com