var data int = 1
var done = make(chan bool)
var last int = 1
var in1 bool = false
var in2 bool = false
var mu sync.Mutex
func racing1() {
for i := 0; i < 10000; i++ {
last = 1
in1 = true
for in2 && last == 1 {
}
// mu.Lock()
data = data + 1
// mu.Unlock()
in1 = false
}
done <- true
}
func racing2() {
for i := 0; i < 10000; i++ {
last = 2
in2 = true
for in1 && last == 2 {
}
// mu.Lock()
data = data + 1
// mu.Unlock()
in2 = false
}
done <- true
}
func main() {
go racing1()
go racing2()
<-done
<-done
fmt.Println(data)
}
Programs that modify data being simultaneously accessed by multiple goroutines must serialize such access.
Yes. I have checked that common languages do not guarantee sequential consistency, if there is data race.
After all, thanks