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:
r does not happen before w.
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:
w happens before r.
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 packageq
, the completion ofq
'sinit
functions happens before the start of any ofp
's.The start of the function
main.main
happens after allinit
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
orsync.RWMutex
variablel
and n < m, call n ofl.Unlock()
happens before call m ofl.Lock()
returns.For any call to
l.RLock
on async.RWMutex
variablel
, there is an n such that thel.RLock
happens (returns) after call n tol.Unlock
and the matchingl.RUnlock
happens before call n+1 tol.Lock
.
Once
A single call of
f()
fromonce.Do(f)
happens (returns) before any call ofonce.Do(f)
returns.
Incorrect synchronization
Last updated