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