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