-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Closed
Description
What steps will reproduce the problem? 1. go run -race the following program: http://play.golang.org/p/8ju-c1opip package main import ( "log" "math" "sync" //"time" ) type T struct { Type string Field string } type U struct { Arg string } func main() { args := []T{ {Type: "constant", Field: "A"}, {Type: "constant", Field: "B"}, {Type: "constant", Field: "C"}, {Type: "constant", Field: "D"}, {Type: "constant", Field: "E"}, {Type: "constant", Field: "F"}, {Type: "constant", Field: "G"}, {Type: "constant", Field: "H"}, } wg := new(sync.WaitGroup) wg.Add(len(args)) for _, arg := range args { log.Printf("doing arg: %+v", arg) local := U{Arg: arg.Field} go func() { defer wg.Done() _ = busy() log.Printf("%+v", local) if arg.Type != "constant" { log.Printf("argh!") } }() //time.Sleep(100 * time.Millisecond) } wg.Wait() } func busy() float64 { x := 0.0 for i := 1; i < 1000000; i++ { x += math.Log(float64(i)) } return x } What is the expected output? What do you see instead? Expected: 2014/07/02 23:40:47 doing arg: {Type:constant Field:A} 2014/07/02 23:40:47 doing arg: {Type:constant Field:B} 2014/07/02 23:40:47 doing arg: {Type:constant Field:C} 2014/07/02 23:40:47 doing arg: {Type:constant Field:D} 2014/07/02 23:40:47 doing arg: {Type:constant Field:E} 2014/07/02 23:40:47 doing arg: {Type:constant Field:F} 2014/07/02 23:40:47 doing arg: {Type:constant Field:G} 2014/07/02 23:40:47 doing arg: {Type:constant Field:H} ================== WARNING: DATA RACE Write by main goroutine: main.main() /tmp/w.go:32 +0x33b Previous read by goroutine 5: main.func·001() /tmp/w.go:39 +0x15f Goroutine 5 (finished) created at: main.main() /tmp/w.go:42 +0x532 ================== 2014/07/02 23:40:47 {Arg:A} 2014/07/02 23:40:47 {Arg:B} 2014/07/02 23:40:47 {Arg:C} 2014/07/02 23:40:47 {Arg:D} 2014/07/02 23:40:47 {Arg:E} 2014/07/02 23:40:47 {Arg:F} 2014/07/02 23:40:47 {Arg:G} 2014/07/02 23:40:47 {Arg:H} Instead: 2014/07/02 23:40:47 doing arg: {Type:constant Field:A} 2014/07/02 23:40:47 doing arg: {Type:constant Field:B} 2014/07/02 23:40:47 doing arg: {Type:constant Field:C} 2014/07/02 23:40:47 doing arg: {Type:constant Field:D} 2014/07/02 23:40:47 doing arg: {Type:constant Field:E} 2014/07/02 23:40:47 doing arg: {Type:constant Field:F} 2014/07/02 23:40:47 doing arg: {Type:constant Field:G} 2014/07/02 23:40:47 doing arg: {Type:constant Field:H} 2014/07/02 23:40:47 {Arg:A} 2014/07/02 23:40:47 {Arg:B} 2014/07/02 23:40:47 {Arg:C} 2014/07/02 23:40:47 {Arg:D} 2014/07/02 23:40:47 {Arg:E} 2014/07/02 23:40:47 {Arg:F} 2014/07/02 23:40:47 {Arg:G} 2014/07/02 23:40:47 {Arg:H} Please use labels and text to provide additional information.