Ich bin interessiert, Korrelationen parallel in Go zu berechnen. Das Hauptproblem, das ich habe, ist, dass alle Go-Prozesse genau dieselbe Berechnung ausführen. Ich habe hier das Problem mit einem sehr einfachen Beispiel reproduziert. I erhalten:Die Verarbeitung von Arrays in Go parallel gibt unerwartete Ergebnisse
4 + 50 = 54
4 + 50 = 54
4 + 50 = 54
statt:
1 + 20 = 21
2 + 30 = 32
3 + 40 = 43
Wenn ich nach oben "wg.Wait()" Ich habe das gute Ergebnis zu erhalten, aber keine Parallelität :( den Dank im Voraus für Ihre Kommentare!
package main
import (
"fmt"
"runtime"
"sync"
)
func process_array(x, y int) int {
r := x + y
return r
}
func main() {
a1 := []int{0, 1, 2, 3, 4}
a2 := []int{10, 20, 30, 40, 50}
runtime.GOMAXPROCS(8)
var wg sync.WaitGroup
for i := 1; i < 4 ; i++ {
wg.Add(1)
go func() {
defer wg.Done()
x :=process_array(a1[i],a2[i])
fmt.Println(a1[i],"+", a2[i],"=", x)
}()
//wg.Wait() give the good result
//but it is not parallel processing
// 1 + 20 = 21
// 2 + 30 = 32
// 3 + 40 = 43
}
wg.Wait() // give a repetition of the same result :
// 4 + 50 = 54
// 4 + 50 = 54
// 4 + 50 = 54
}
FAQ: https://golang.org/doc/faq#closures_and_goroutines – JimB
Beachten Sie, dass praktisch, Sie möchten Ihre goroutines geben größere Aufgaben (zB, wenn Sie N cpus haben, geben Sie jede 1/n zu tun). Eine Ergänzung ist sehr schnell, und die Koordination zum Starten eines Threads und so weiter dauert logner. – twotwotwo