【发布时间】:2017-03-05 03:21:44
【问题描述】:
我正在编写一个算法来将图像分解为多个片段并对其进行操作,但是我目前使用 Go 例程的方式并不是很理想。
我想将其拆分为一个工作池,启动例程并让每个工作人员接受新工作,直到完成映像。
我把它分成 8 个这样:
var bounds = img.Bounds()
var halfHeight = bounds.Max.Y / 2
var eighthOne = halfHeight / 4
var eighthTwo = eighthOne + eighthOne
var eighthThree = eighthOne + eighthTwo
var eighthFive = halfHeight + eighthOne
var eighthSix = halfHeight + eighthTwo
var eighthSeven = halfHeight + eighthThree
elapsed := time.Now()
go Threshold(pic, c2, 0, eighthOne)
go Threshold(pic, c5, eighthOne, eighthTwo)
go Threshold(pic, c6, eighthTwo, eighthThree)
go Threshold(pic, c7, eighthThree, halfHeight)
go Threshold(pic, c8, halfHeight, eighthFive)
go Threshold(pic, c9, eighthFive, eighthSix)
go Threshold(pic, c10, eighthSix, eighthSeven)
go Threshold(pic, c11, eighthSeven, bounds.Max.Y)
然后我一个接一个地启动 Go 例程,如何将其优化为工作系统?
谢谢
【问题讨论】:
-
答案肯定是缓冲通道。如果您还没有经历过,那么 go tour 很好地解释了 go 并发原语。 tour.golang.org/concurrency/2
标签: go