This is a creation in Article, where the information may have evolved or changed.
Introduction
The Go memory model specifies the conditions under which reads of a variable in one goroutine can is guaranteed to observe Values produced by writes to the same variable in a different goroutine.
Happens before
Within a single goroutine, reads and writes must behave as if they executed in the order specified by the program. That's, compilers and processors may reorder the reads and writes executed within a single goroutine only when the Reorde Ring does not a change of the behavior within that goroutine as defined by the language specification. Because of this reordering, the execution order observed by one goroutine could differ from the order perceived by another. For example, if one goroutine executes a = 1; b = 2; another might observe the updated value of B before the updated value of a.
To specify the requirements of reads and writes, we define happens before, a partial order on the execution of memory oper Ations in a Go program. If Event E1 happens before event E2, then we say the 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 was 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:
W happens before R. There is no other write W ' to V so happens after W but before R. To guarantee. A read r of a variable v observes a particular write W to V, ensure that W are the only write R is Allowe D to observe. That's, R is guaranteed to observe w if both of the following hold:
Within a single goroutine, there was no concurrency, so the other definitions is EQUIVALENT:A read R observes the value WRI Tten 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 cond Itions that ensure reads observe the desired writes.
The initialization of variable v with the zero value for V ' s type behaves as a write in the memory model.
Reads and writes of values larger than a single machine word behave as multiple machine-word-sized operations in an unspec ified order.
Synchronization
Initialization
Program initialization runs in a single goroutine and new goroutines created during initialization does not start running UN Til initialization ends.
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 has finished.
The execution of any goroutines created during Init functions happens after all init functions has finished.
Goroutine creation
The GO statement that starts a new goroutine happens before the Goroutine ' s execution begins.
For example, the This program:
var a stringfunc f() { print(a)}func hello() { a = "hello, world" go f()}
Calling Hello would print "Hello, world" at some-in-the-the-perhaps after hello have returned.
Goroutine Destruction
The exit of a goroutine is not guaranteed to happen before all event in the program. For example, the This program:
var a stringfunc hello() { go func() { a = "hello" }() print(a)}
The assignment to a isn't followed by any synchronization event, so it's not guaranteed to being observed by any other Goro Utine. In fact, an aggressive compiler might delete the entire GO statement.
If the effects of a goroutine must be observed by another goroutine, use a synchronization mechanism such as a lock or Cha Nnel communication to establish a relative ordering.
Channel Communication
Channel communication is the main method of synchronization between Goroutines. Each send on a particular channel are matched to a corresponding receive from the channel, usually in a different goroutin E.
A Send on a channel happens before the corresponding receive from that channel completes.
This program:
var c = make (chan int, 10)
var a string
Func f () {
a = "hello, world"c <- 0
}
Func Main () {
go f()<-cprint(a)
}
is guaranteed to print "Hello, World". The write to a happens before the Send in C, which happens before the corresponding receive on C completes, which happens Before the print.
The closing of a channel happens before a receive that returns a zero value because the channel is closed.
In the previous example, replacing C <-0 with close (c) yields a program with the same guaranteed behavior.
A receive from A unbuffered channel happens before the send to that channel completes.
This program (as above, but with the send and receive statements swapped and using a unbuffered channel):
var c = make (chan int)
var a string
Func f () {
a = "hello, world"<-c
}
Func Main () {
go f()c <- 0print(a)
}
is also guaranteed to print "Hello, World". The write to a happens before the receive in C, which happens before the corresponding Send on C completes, which happens Before the print.
If the channel were buffered (e.g., C = Make (chan int, 1) and then the program would isn't being guaranteed to print "Hello, world ". (It might print the empty string; it cannot print "Goodbye, Universe", nor can It crash.)
Locks
The sync package implements the data types, sync. Mutex and sync. Rwmutex.
For any sync. Mutex or sync. Rwmutex variable L and N < m, the n ' th call to L.unlock () happens before the M ' th call to L.lock () returns.
This program:
var l sync. Mutex
var a string
Func f () {
a = "hello, world"l.Unlock()
}
Func Main () {
l.Lock()go f()l.Lock()print(a)
}
is guaranteed to print "Hello, World". The first call to L.unlock () (in F) happens before the second call to L.lock () (in main) returns, which happens before the Print.
For any call to L.rlock on a sync. Rwmutex variable L, there is an n such then the L.rlock happens (returns) after the n ' th call to L.unlock and the matching L.runlock happens before the n+1 ' th call to L.lock.
Once
The sync package provides a safe mechanism for initialization in the presence of multiple goroutines through the use of th e Once type. Multiple threads can execute once. Do (f) for a particular F, if only one would run F (), and the other calls block until F () have returned.
A single Call of F () from once. Do (f) happens (returns) before any call of once. Do (f) returns.
In the program:
var a string
var once sync. Once
Func Setup () {
a = "hello, world"
}
Func Doprint () {
once.Do(setup)print(a)
}
Func Twoprint () {
go doprint()go doprint()
}
Calling Twoprint causes "Hello, world" to be printed twice. The first call to Twoprint runs Setup once.
Incorrect synchronization
Note that a read R is observe the value written by a write W, happens concurrently with R. Even if this occurs, the it does not imply the reads happening after R would observe writes that happened before W.
In the program:
var a, b int
Func f () {
a = 1b = 2
}
Func g () {
print(b)print(a)
}
Func Main () {
go f()g()
}
It can happen that G prints 2 and then 0.
This fact invalidates a few common idioms.
Double-checked locking is a attempt to avoid the overhead of synchronization. For example, the Twoprint program might is incorrectly written as:
var a string
var done bool
Func Setup () {
a = "hello, world"done = true
}
Func Doprint () {
if !done { once.Do(setup)}print(a)
}
Func Twoprint () {
go doprint()go doprint()
}
But there was no guarantee that, in Doprint, observing the write-to-do implies observing the write to a. This version can (incorrectly) print a empty string instead of "Hello, World".
Another incorrect idiom is busy waiting for a value, as in:
var a string
var done bool
Func Setup () {
a = "hello, world"done = true
}
Func Main () {
go setup()for !done {}print(a)
}
As before, there is no guarantee, in main, observing the write to do implies observing the write to a, so this prog Ram could print an empty string too. Worse, there is no guarantee then the write to done would ever be observed by main, since there was no synchronization even TS between the threads. The loop in main isn't guaranteed to finish.
There is subtler variants on this theme, such as this program.
Type T struct {
msg string
}
var g *t
Func Setup () {
t := new(T)t.msg = "hello, world"g = t
}
Func Main () {
go setup()for g == nil {}print(g.msg)
}
Even if main observes g! = nil and exits its loop, there are no guarantee that it'll observe the initialized value for G. Msg.
In all these examples, the solution is the Same:use explicit synchronization.