在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Version of May 31, 2014
IntroductionThe 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. AdvicePrograms 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 If you must read the rest of this document to understand the behavior of your program, you are being too clever. Don't be clever. Happens BeforeWithin 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. Because of this reordering, the execution order observed by one goroutine may differ from the order perceived by another. For example, if one goroutine executes 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
To guarantee that a read r of a variable
This pair of conditions is stronger than the first pair; it requires that there are no other writes happening concurrently with w or 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 The initialization of variable Reads and writes of values larger than a single machine word behave as multiple machine-word-sized operations in an unspecified order. SynchronizationInitializationProgram initialization runs in a single goroutine, but that goroutine may create other goroutines, which run concurrently. If a package The start of the function Goroutine creationThe For example, in this program: var a string func f() { print(a) } func hello() { a = "hello, world" go f() } calling Goroutine destructionThe exit of a goroutine is not guaranteed to happen before any event in the program. For example, in this program: var a string func hello() { go func() { a = "hello" }() print(a) } the assignment to 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 communicationChannel communication is the main method of synchronization between goroutines. Each send on a particular channel is matched to a corresponding receive from that channel, usually in a different goroutine. 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() <-c print(a) } is guaranteed to 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 A receive from an unbuffered channel happens before the send on that channel completes. This program (as above, but with the send and receive statements swapped and using an unbuffered channel): var c = make(chan int) var a string func f() { a = "hello, world" <-c } func main() { go f() c <- 0 print(a) } is also guaranteed to print If the channel were buffered (e.g., The kth receive on a channel with capacity C happens before the k+Cth send from that channel completes. This rule generalizes the previous rule to buffered channels. It allows a counting semaphore to be modeled by a buffered channel: the number of items in the channel corresponds to the number of active uses, the capacity of the channel corresponds to the maximum number of simultaneous uses, sending an item acquires the semaphore, and receiving an item releases the semaphore. This is a common idiom for limiting concurrency. This program starts a goroutine for every entry in the work list, but the goroutines coordinate using the var limit = make(chan int, 3) func main() { for _, w := range work { go func(w func()) { limit <- 1 w() <-limit }(w) } select{} } LocksThe For any 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 For any call to OnceThe A single call of In this 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 Incorrect synchronizationNote that a read r may observe the value written by a write w that happens concurrently with r. Even if this occurs, it does not imply that reads happening after r will observe writes that happened before w. In this program: var a, b int func f() { a = 1 b = 2 } func g() { print(b) print(a) } func main() { go f() g() } it can happen that This fact invalidates a few common idioms. Double-checked locking is an attempt to avoid the overhead of synchronization. For example, the 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 is no guarantee that, in 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 that, in There are 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 In all these examples, the solution is the same: use explicit synchronization. |
请发表评论