Consider following code:
package main
import (
"fmt"
"sync"
)
func main() {
var a int
m := new(sync.Mutex)
wg := sync.WaitGroup{}
wg.Add(2)
go func(){
m.Lock()
a = 2
m.Unlock()
wg.Done()
}()
go func(){
//m.Lock()
a = 9
//m.Unlock()
wg.Done()
}()
wg.Wait()
fmt.Println(a)
}
if we run this code with -race flag we get warning that there is a race condition.
1) what can possibly go wrong with this race condition?
if we uncomment lock in second routine we don't get a race condition warning. But we can have different output so a race condition exists.
2) Why now we don't have a race condition warning?