管道的使用介紹併發
如今要計算 1-N 的各個數的階乘,而且把各個數的階乘放入到 map 中。最後顯示出來。要求使用 goroutine 完成性能
package main import ( "fmt" ) var ( mymap = make(map[int]int, 10) ) func calc(n int) { res := 1 for i := 1; i <= n; i++ { res *= i } mymap[n] = res } func main() { for n := 1; n < 20; n++ { go calc(n) } for key, val := range mymap { fmt.Printf("map[%d]=%d\n", key, val) } }
問題1:使用goroutine時,主線程執行結束,即便協程沒有執行完畢也會結束,顧map沒有結果spa
func main() { for n := 1; n < 20; n++ { go calc(n) } time.Sleep(time.Second * 3) // 加入睡眠 for key, val := range mymap { fmt.Printf("map[%d]=%d\n", key, val) } }
問題2:fatal error:併發寫線程
API server listening at: 127.0.0.1:4417 fatal error: concurrent map writes goroutine 36 [running]: runtime.throw(0x4e0144, 0x15) D:/programs/go/src/runtime/panic.go:608 +0x79 fp=0xc0000bff28 sp=0xc0000bfef8 pc=0x42e099 runtime.mapassign_fast64(0x4c2300, 0xc000072240, 0x12, 0x0) D:/programs/go/src/runtime/map_fast64.go:101 +0x41a fp=0xc0000bff90 sp=0xc0000bff28 pc=0x4110ea main.calc(0x12) D:/go_work/src/gortoutine/1/1.go:18 +0x94 fp=0xc0000bffd8 sp=0xc0000bff90 pc=0x4aa314 runtime.goexit() D:/programs/go/src/runtime/asm_amd64.s:1333 +0x1 fp=0xc0000bffe0 sp=0xc0000bffd8 pc=0x456f41 created by main.main D:/go_work/src/gortoutine/1/1.go:23 +0x70 goroutine 1 [sleep]: time.Sleep(0xb2d05e00) D:/programs/go/src/runtime/time.go:105 +0x132 main.main() D:/go_work/src/gortoutine/1/1.go:26 +0x8f
排查方法:在運行某個程序時,如何知道是否存在資源競爭問題。 方法很簡單,在編譯該程序時,增長一個參數 -race 便可server
D:\go_work>1.exe ================== WARNING: DATA RACE Write at 0x00c00006e240 by goroutine 7: runtime.mapassign_fast64() D:/programs/go/src/runtime/map_fast64.go:92 +0x0 main.calc() D:/go_work/src/goroutine/1/1.go:18 +0x91 Previous write at 0x00c00006e240 by goroutine 6: runtime.mapassign_fast64() D:/programs/go/src/runtime/map_fast64.go:92 +0x0 main.calc() D:/go_work/src/goroutine/1/1.go:18 +0x91 Goroutine 7 (running) created at: main.main() D:/go_work/src/goroutine/1/1.go:23 +0x6f Goroutine 6 (finished) created at: main.main() D:/go_work/src/goroutine/1/1.go:23 +0x6f ================== map[5]=120 map[6]=720 map[8]=40320 map[13]=6227020800 ================== WARNING: DATA RACE Read at 0x00c0000b6068 by main goroutine: main.main() D:/go_work/src/goroutine/1/1.go:27 +0x103 Previous write at 0x00c0000b6068 by goroutine 19: main.calc() D:/go_work/src/goroutine/1/1.go:18 +0xa6 Goroutine 19 (finished) created at: main.main() D:/go_work/src/goroutine/1/1.go:23 +0x6f ================== map[14]=87178291200 map[4]=24 map[10]=3628800 map[16]=20922789888000 map[18]=6402373705728000 map[17]=355687428096000 map[19]=121645100408832000 map[1]=1 map[2]=2 map[7]=5040 map[11]=39916800 map[12]=479001600 map[15]=1307674368000 map[3]=6 map[9]=362880 Found 2 data race(s)
會提示存在兩個數據競爭協程
低水平解決方法,給變量加鎖。blog
package main import ( "fmt" "sync" "time" ) var ( mymap = make(map[int]int, 10) lock sync.Mutex ) func calc(n int) { res := 1 for i := 1; i <= n; i++ { res *= i } lock.Lock() mymap[n] = res lock.Unlock() } func main() { for n := 1; n < 20; n++ { go calc(n) } time.Sleep(time.Second * 10) for key, val := range mymap { lock.Lock() fmt.Printf("map[%d]=%d\n", key, val) lock.Unlock() } }
這是個笨方法,大大影響程序性能資源