我試圖使用Go的併發並行運行幾個算了一筆賬:去例程和渠道去
func intensity_calc(input Matrix, distance float64) Matrix {
output := create_matrix(len(input), len(input[0]))
var wg sync.WaitGroup
reverse := len(input)
wg.Add(len(input)/2)
for i := 0; i < len(input)/2; i++ {
output[i][x_ln] = input[i][x_ln]
go func() { // creates a go-routine
points <- contributions_sum(input, distance, input[i][x_ln])
output[i][y_ln] = <-points
output[reverse][y_ln] = output[i][y_ln]
fmt.Println(i)
defer wg.Done() // process is done
}()
}
wg.Wait() // wait until all processes are finished
return output
}
* 輸出是一個二維數組
代碼設取值從數組輸入將它們發送到函數,該函數將值返回到點。 信道是全局定義:
var points chan float64
並在main()函數:
points = make(chan float64)
但總是收到此錯誤:
goroutine 2017 [chan send]:
main.intensity_calc.func1(0xc04206a000, 0xfa1, 0xfa1, 0x3f50624dd2f1a9fc, 0xc0420bb660, 0xc042094000, 0xfa1, 0xfa1, 0xfa1, 0xc0420bb650)
C:/.../go concurrent calculation.go:71 +0xbf
created by main.intensity_calc
C:/.../go concurrent calculation.go:76 +0x1c0
請注意併發!=並行處理。 – Flimzy