Skip to content

Instantly share code, notes, and snippets.

@zhyon404
Created February 2, 2019 06:51
Show Gist options
  • Save zhyon404/21e257d833bfbdd59f709a6dc5b9c79d to your computer and use it in GitHub Desktop.
Save zhyon404/21e257d833bfbdd59f709a6dc5b9c79d to your computer and use it in GitHub Desktop.
func Test_double_check_unsafe(t *testing.T) {
m := make(map[int]int64)
mu := sync.Mutex{}
wg := sync.WaitGroup{}
wg.Add(100 * 10)
for i := 0; i < 100; i++ {
for j := 0; j < 10; j++ {
go func(j int) {
defer wg.Done()
v := m[j]
if v != 0 {
return
}
mu.Lock()
defer mu.Unlock()
mm := make(map[int]int64)
mm[j] = 1
m = mm
}(1)
}
}
wg.Wait()
}
@zhyon404
Copy link
Author

zhyon404 commented Feb 2, 2019

WARNING: DATA RACE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment