1 回答
TA贡献1827条经验 获得超8个赞
有2个问题:
你实际上并没有等待你的 goroutines 完成。
您不会传递
i and j给 goroutine,因此它们几乎总是最后一个 i 和 j。
您的函数应该类似于:
func Julia(f ComplexFunc, n int) image.Image {
var wg sync.WaitGroup
bounds := image.Rect(-n/2, -n/2, n/2, n/2)
img := image.NewRGBA(bounds)
s := float64(n / 4)
for i := bounds.Min.X; i < bounds.Max.X; i++ {
for j := bounds.Min.Y; j < bounds.Max.Y; j++ {
wg.Add(1)
go func(i, j int) {
n := Iterate(f, complex(float64(i)/s, float64(j)/s), 256)
r := uint8(0)
g := uint8(0)
b := uint8(n % 32 * 8)
img.Set(i, j, color.RGBA{r, g, b, 255})
wg.Done()
}(i, j)
}
}
wg.Wait()
return img
}
奖金提示,当潜入并发性,它通常是一个好主意,尝试用你的代码的比赛探测器。
您可能必须使用互斥锁进行调用,img.Set但我不太确定,也无法测试 atm。
- 1 回答
- 0 关注
- 214 浏览
添加回答
举报
