在此输入代码...// this works

package main

import (
   "sync"
   "sync/atomic"
   "time"
)

var a int32
var done int32
var once sync.Once

func setup() {
   a = 22222
   // write
   atomic.StoreInt32(&done, 1)
}

func doprint() {
   if done == 0 { // if we use atomic to get value here, it's all fine. but 
why? read a bool variable should be atomic
      once.Do(setup)
   }
   // read
   println("get", a)
}

func twoprint() {
   go doprint()
   go doprint()
}

func main() {
   twoprint()
   time.Sleep(time.Hour)
}


i do a little change on https://golang.org/ref/mem incorrect useage 
synchronization session. 

it really bother me why this code have data race, i see this kind of code in 
runtime.channel, it also just use channel.closed without lock. 


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/1ec5a62f-81fa-4ae4-8c20-18b533cb2207%40googlegroups.com.

Reply via email to