|
|
|
|
|
by jrockway
2244 days ago
|
|
I can convince myself that data races need not be a race condition. Consider this simple program: var i int
doneCh := make(chan struct{})
go func() { i = 1; doneCh <- struct{}{} }() // a
go func() { i = 1; doneCh <- struct{}{} }() // b
<-doneCh
<-doneCh
At the end of the program, i is always equal to 1 no matter which order a or b wrote to i. But it's a race because you are assigning to a shared variable without synchronization. A small modification to the program creates a race condition: var i int
doneCh := make(chan struct{})
go func() { i = 1; doneCh <- struct{}{} }() // a
go func() { i = 2; doneCh <- struct{}{} }() // b
<-doneCh
<-doneCh
Is i 1 or 2? It depends.It is correct for the race checker to complain about the first program, because after a bit of hacking the first program can very easily change into the second program. (And I tried it, and it does complain.) |
|