blob: 143f3b2ff22e9d52047b05954f8e37aa1ff19b50 [file] [log] [blame]
Colin Cross7bb052a2015-02-03 12:59:37 -08001<!--{
2 "Title": "The Go Memory Model",
3 "Subtitle": "Version of May 31, 2014",
4 "Path": "/ref/mem"
5}-->
6
7<style>
8p.rule {
9 font-style: italic;
10}
11span.event {
12 font-style: italic;
13}
14</style>
15
16<h2>Introduction</h2>
17
18<p>
19The Go memory model specifies the conditions under which
20reads of a variable in one goroutine can be guaranteed to
21observe values produced by writes to the same variable in a different goroutine.
22</p>
23
24
25<h2>Advice</h2>
26
27<p>
28Programs that modify data being simultaneously accessed by multiple goroutines
29must serialize such access.
30</p>
31
32<p>
33To serialize access, protect the data with channel operations or other synchronization primitives
34such as those in the <a href="/pkg/sync/"><code>sync</code></a>
35and <a href="/pkg/sync/atomic/"><code>sync/atomic</code></a> packages.
36</p>
37
38<p>
39If you must read the rest of this document to understand the behavior of your program,
40you are being too clever.
41</p>
42
43<p>
44Don't be clever.
45</p>
46
47<h2>Happens Before</h2>
48
49<p>
50Within a single goroutine, reads and writes must behave
51as if they executed in the order specified by the program.
52That is, compilers and processors may reorder the reads and writes
53executed within a single goroutine only when the reordering
54does not change the behavior within that goroutine
55as defined by the language specification.
56Because of this reordering, the execution order observed
57by one goroutine may differ from the order perceived
58by another. For example, if one goroutine
59executes <code>a = 1; b = 2;</code>, another might observe
60the updated value of <code>b</code> before the updated value of <code>a</code>.
61</p>
62
63<p>
64To specify the requirements of reads and writes, we define
65<i>happens before</i>, a partial order on the execution
66of memory operations in a Go program. If event <span class="event">e<sub>1</sub></span> happens
67before event <span class="event">e<sub>2</sub></span>, then we say that <span class="event">e<sub>2</sub></span> happens after <span class="event">e<sub>1</sub></span>.
68Also, if <span class="event">e<sub>1</sub></span> does not happen before <span class="event">e<sub>2</sub></span> and does not happen
69after <span class="event">e<sub>2</sub></span>, then we say that <span class="event">e<sub>1</sub></span> and <span class="event">e<sub>2</sub></span> happen concurrently.
70</p>
71
72<p class="rule">
73Within a single goroutine, the happens-before order is the
74order expressed by the program.
75</p>
76
77<p>
78A read <span class="event">r</span> of a variable <code>v</code> is <i>allowed</i> to observe a write <span class="event">w</span> to <code>v</code>
79if both of the following hold:
80</p>
81
82<ol>
83<li><span class="event">r</span> does not happen before <span class="event">w</span>.</li>
84<li>There is no other write <span class="event">w'</span> to <code>v</code> that happens
85 after <span class="event">w</span> but before <span class="event">r</span>.</li>
86</ol>
87
88<p>
89To guarantee that a read <span class="event">r</span> of a variable <code>v</code> observes a
90particular write <span class="event">w</span> to <code>v</code>, ensure that <span class="event">w</span> is the only
91write <span class="event">r</span> is allowed to observe.
92That is, <span class="event">r</span> is <i>guaranteed</i> to observe <span class="event">w</span> if both of the following hold:
93</p>
94
95<ol>
96<li><span class="event">w</span> happens before <span class="event">r</span>.</li>
97<li>Any other write to the shared variable <code>v</code>
98either happens before <span class="event">w</span> or after <span class="event">r</span>.</li>
99</ol>
100
101<p>
102This pair of conditions is stronger than the first pair;
103it requires that there are no other writes happening
104concurrently with <span class="event">w</span> or <span class="event">r</span>.
105</p>
106
107<p>
108Within a single goroutine,
109there is no concurrency, so the two definitions are equivalent:
110a read <span class="event">r</span> observes the value written by the most recent write <span class="event">w</span> to <code>v</code>.
111When multiple goroutines access a shared variable <code>v</code>,
112they must use synchronization events to establish
113happens-before conditions that ensure reads observe the
114desired writes.
115</p>
116
117<p>
118The initialization of variable <code>v</code> with the zero value
119for <code>v</code>'s type behaves as a write in the memory model.
120</p>
121
122<p>
123Reads and writes of values larger than a single machine word
124behave as multiple machine-word-sized operations in an
125unspecified order.
126</p>
127
128<h2>Synchronization</h2>
129
130<h3>Initialization</h3>
131
132<p>
133Program initialization runs in a single goroutine,
134but that goroutine may create other goroutines,
135which run concurrently.
136</p>
137
138<p class="rule">
139If a package <code>p</code> imports package <code>q</code>, the completion of
140<code>q</code>'s <code>init</code> functions happens before the start of any of <code>p</code>'s.
141</p>
142
143<p class="rule">
144The start of the function <code>main.main</code> happens after
145all <code>init</code> functions have finished.
146</p>
147
148<h3>Goroutine creation</h3>
149
150<p class="rule">
151The <code>go</code> statement that starts a new goroutine
152happens before the goroutine's execution begins.
153</p>
154
155<p>
156For example, in this program:
157</p>
158
159<pre>
160var a string
161
162func f() {
163 print(a)
164}
165
166func hello() {
167 a = "hello, world"
168 go f()
169}
170</pre>
171
172<p>
173calling <code>hello</code> will print <code>"hello, world"</code>
174at some point in the future (perhaps after <code>hello</code> has returned).
175</p>
176
177<h3>Goroutine destruction</h3>
178
179<p>
180The exit of a goroutine is not guaranteed to happen before
181any event in the program. For example, in this program:
182</p>
183
184<pre>
185var a string
186
187func hello() {
188 go func() { a = "hello" }()
189 print(a)
190}
191</pre>
192
193<p>
194the assignment to <code>a</code> is not followed by
195any synchronization event, so it is not guaranteed to be
196observed by any other goroutine.
197In fact, an aggressive compiler might delete the entire <code>go</code> statement.
198</p>
199
200<p>
201If the effects of a goroutine must be observed by another goroutine,
202use a synchronization mechanism such as a lock or channel
203communication to establish a relative ordering.
204</p>
205
206<h3>Channel communication</h3>
207
208<p>
209Channel communication is the main method of synchronization
210between goroutines. Each send on a particular channel
211is matched to a corresponding receive from that channel,
212usually in a different goroutine.
213</p>
214
215<p class="rule">
216A send on a channel happens before the corresponding
217receive from that channel completes.
218</p>
219
220<p>
221This program:
222</p>
223
224<pre>
225var c = make(chan int, 10)
226var a string
227
228func f() {
229 a = "hello, world"
230 c &lt;- 0
231}
232
233func main() {
234 go f()
235 &lt;-c
236 print(a)
237}
238</pre>
239
240<p>
241is guaranteed to print <code>"hello, world"</code>. The write to <code>a</code>
242happens before the send on <code>c</code>, which happens before
243the corresponding receive on <code>c</code> completes, which happens before
244the <code>print</code>.
245</p>
246
247<p class="rule">
248The closing of a channel happens before a receive that returns a zero value
249because the channel is closed.
250</p>
251
252<p>
253In the previous example, replacing
254<code>c &lt;- 0</code> with <code>close(c)</code>
255yields a program with the same guaranteed behavior.
256</p>
257
258<p class="rule">
259A receive from an unbuffered channel happens before
260the send on that channel completes.
261</p>
262
263<p>
264This program (as above, but with the send and receive statements swapped and
265using an unbuffered channel):
266</p>
267
268<pre>
269var c = make(chan int)
270var a string
271
272func f() {
273 a = "hello, world"
274 &lt;-c
275}
276</pre>
277
278<pre>
279func main() {
280 go f()
281 c &lt;- 0
282 print(a)
283}
284</pre>
285
286<p>
287is also guaranteed to print <code>"hello, world"</code>. The write to <code>a</code>
288happens before the receive on <code>c</code>, which happens before
289the corresponding send on <code>c</code> completes, which happens
290before the <code>print</code>.
291</p>
292
293<p>
294If the channel were buffered (e.g., <code>c = make(chan int, 1)</code>)
295then the program would not be guaranteed to print
296<code>"hello, world"</code>. (It might print the empty string,
297crash, or do something else.)
298</p>
299
300<p class="rule">
301The <i>k</i>th receive on a channel with capacity <i>C</i> happens before the <i>k</i>+<i>C</i>th send from that channel completes.
302</p>
303
304<p>
305This rule generalizes the previous rule to buffered channels.
306It allows a counting semaphore to be modeled by a buffered channel:
307the number of items in the channel corresponds to the number of active uses,
308the capacity of the channel corresponds to the maximum number of simultaneous uses,
309sending an item acquires the semaphore, and receiving an item releases
310the semaphore.
311This is a common idiom for limiting concurrency.
312</p>
313
314<p>
315This program starts a goroutine for every entry in the work list, but the
316goroutines coordinate using the <code>limit</code> channel to ensure
317that at most three are running work functions at a time.
318</p>
319
320<pre>
321var limit = make(chan int, 3)
322
323func main() {
324 for _, w := range work {
Dan Willemsen6ff23252015-09-15 13:49:18 -0700325 go func(w func()) {
326 limit &lt;- 1
Colin Cross7bb052a2015-02-03 12:59:37 -0800327 w()
Dan Willemsen6ff23252015-09-15 13:49:18 -0700328 &lt;-limit
329 }(w)
Colin Cross7bb052a2015-02-03 12:59:37 -0800330 }
331 select{}
332}
333</pre>
334
335<h3>Locks</h3>
336
337<p>
338The <code>sync</code> package implements two lock data types,
339<code>sync.Mutex</code> and <code>sync.RWMutex</code>.
340</p>
341
342<p class="rule">
343For any <code>sync.Mutex</code> or <code>sync.RWMutex</code> variable <code>l</code> and <i>n</i> &lt; <i>m</i>,
344call <i>n</i> of <code>l.Unlock()</code> happens before call <i>m</i> of <code>l.Lock()</code> returns.
345</p>
346
347<p>
348This program:
349</p>
350
351<pre>
352var l sync.Mutex
353var a string
354
355func f() {
356 a = "hello, world"
357 l.Unlock()
358}
359
360func main() {
361 l.Lock()
362 go f()
363 l.Lock()
364 print(a)
365}
366</pre>
367
368<p>
369is guaranteed to print <code>"hello, world"</code>.
370The first call to <code>l.Unlock()</code> (in <code>f</code>) happens
371before the second call to <code>l.Lock()</code> (in <code>main</code>) returns,
372which happens before the <code>print</code>.
373</p>
374
375<p class="rule">
376For any call to <code>l.RLock</code> on a <code>sync.RWMutex</code> variable <code>l</code>,
377there is an <i>n</i> such that the <code>l.RLock</code> happens (returns) after call <i>n</i> to
378<code>l.Unlock</code> and the matching <code>l.RUnlock</code> happens
379before call <i>n</i>+1 to <code>l.Lock</code>.
380</p>
381
382<h3>Once</h3>
383
384<p>
385The <code>sync</code> package provides a safe mechanism for
386initialization in the presence of multiple goroutines
387through the use of the <code>Once</code> type.
388Multiple threads can execute <code>once.Do(f)</code> for a particular <code>f</code>,
389but only one will run <code>f()</code>, and the other calls block
390until <code>f()</code> has returned.
391</p>
392
393<p class="rule">
394A single call of <code>f()</code> from <code>once.Do(f)</code> happens (returns) before any call of <code>once.Do(f)</code> returns.
395</p>
396
397<p>
398In this program:
399</p>
400
401<pre>
402var a string
403var once sync.Once
404
405func setup() {
406 a = "hello, world"
407}
408
409func doprint() {
410 once.Do(setup)
411 print(a)
412}
413
414func twoprint() {
415 go doprint()
416 go doprint()
417}
418</pre>
419
420<p>
421calling <code>twoprint</code> causes <code>"hello, world"</code> to be printed twice.
422The first call to <code>doprint</code> runs <code>setup</code> once.
423</p>
424
425<h2>Incorrect synchronization</h2>
426
427<p>
428Note that a read <span class="event">r</span> may observe the value written by a write <span class="event">w</span>
429that happens concurrently with <span class="event">r</span>.
430Even if this occurs, it does not imply that reads happening after <span class="event">r</span>
431will observe writes that happened before <span class="event">w</span>.
432</p>
433
434<p>
435In this program:
436</p>
437
438<pre>
439var a, b int
440
441func f() {
442 a = 1
443 b = 2
444}
445
446func g() {
447 print(b)
448 print(a)
449}
450
451func main() {
452 go f()
453 g()
454}
455</pre>
456
457<p>
458it can happen that <code>g</code> prints <code>2</code> and then <code>0</code>.
459</p>
460
461<p>
462This fact invalidates a few common idioms.
463</p>
464
465<p>
466Double-checked locking is an attempt to avoid the overhead of synchronization.
467For example, the <code>twoprint</code> program might be
468incorrectly written as:
469</p>
470
471<pre>
472var a string
473var done bool
474
475func setup() {
476 a = "hello, world"
477 done = true
478}
479
480func doprint() {
481 if !done {
482 once.Do(setup)
483 }
484 print(a)
485}
486
487func twoprint() {
488 go doprint()
489 go doprint()
490}
491</pre>
492
493<p>
494but there is no guarantee that, in <code>doprint</code>, observing the write to <code>done</code>
495implies observing the write to <code>a</code>. This
496version can (incorrectly) print an empty string
497instead of <code>"hello, world"</code>.
498</p>
499
500<p>
501Another incorrect idiom is busy waiting for a value, as in:
502</p>
503
504<pre>
505var a string
506var done bool
507
508func setup() {
509 a = "hello, world"
510 done = true
511}
512
513func main() {
514 go setup()
515 for !done {
516 }
517 print(a)
518}
519</pre>
520
521<p>
522As before, there is no guarantee that, in <code>main</code>,
523observing the write to <code>done</code>
524implies observing the write to <code>a</code>, so this program could
525print an empty string too.
526Worse, there is no guarantee that the write to <code>done</code> will ever
527be observed by <code>main</code>, since there are no synchronization
528events between the two threads. The loop in <code>main</code> is not
529guaranteed to finish.
530</p>
531
532<p>
533There are subtler variants on this theme, such as this program.
534</p>
535
536<pre>
537type T struct {
538 msg string
539}
540
541var g *T
542
543func setup() {
544 t := new(T)
545 t.msg = "hello, world"
546 g = t
547}
548
549func main() {
550 go setup()
551 for g == nil {
552 }
553 print(g.msg)
554}
555</pre>
556
557<p>
558Even if <code>main</code> observes <code>g != nil</code> and exits its loop,
559there is no guarantee that it will observe the initialized
560value for <code>g.msg</code>.
561</p>
562
563<p>
564In all these examples, the solution is the same:
565use explicit synchronization.
566</p>