-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Closed
Labels
Description
I run the below code with race detector
package main
import (
"sync"
)
func main() {
var m *sync.WaitGroup
m = &sync.WaitGroup{}
for i := 0; i < 100; i++ {
go func(m *sync.WaitGroup) {
m.Add(1)
}(m)
}
m.Wait()
}
and got a data race warning:
WARNING: DATA RACE
Write at 0x00c04200e03c by main goroutine:
internal/race.Write()
D:/Go/src/internal/race/race.go:41 +0x3f
sync.(*WaitGroup).Wait()
D:/Go/src/sync/waitgroup.go:129 +0xfb
main.main()
C:/Users/Lifang/go/src/ago.go:15 +0xbb
Previous read at 0x00c04200e03c by goroutine 5:
internal/race.Read()
D:/Go/src/internal/race/race.go:37 +0x3f
sync.(*WaitGroup).Add()
D:/Go/src/sync/waitgroup.go:71 +0x176
main.main.func1()
C:/Users/Lifang/go/src/ago.go:12 +0x48
I am confused.. Is it the Add() function should not call in other goroutine?