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