我试图并行进行一些计算.该程序的设计使每个工作者goroutine将已解决的谜题的"碎片"发送回控制器goroutine,等待接收和组装从工作程序发送的所有内容.
什么是关闭单一频道的idomatic Go?我不能在每个goroutine的频道上打电话,因为那时我可以发送一个封闭的频道.同样,没有办法预先确定哪个goroutine会先完成.这里需要sync.WaitGroup吗?
这是一个使用sync.WaitGroup
你要做的事情的例子,
这个例子接受一个长整的整数列表,然后通过将N个并行工作者交给一个相等大小的输入数据块来对它们进行求和.它可以在游乐场上运行:
package main
import (
"fmt"
"sync"
)
const WorkerCount = 10
func main() {
// Some input data to operate on.
// Each worker gets an equal share to work on.
data := make([]int, WorkerCount*10)
for i := range data {
data[i] = i
}
// Sum all the entries.
result := sum(data)
fmt.Printf("Sum: %d\n", result)
}
// sum adds up the numbers in the given list, by having the operation delegated
// to workers operating in parallel on sub-slices of the input data.
func sum(data []int) int {
var sum int
result := make(chan int)
defer close(result)
// Accumulate results from workers.
go func() {
for {
select {
case value := <-result:
sum += value
}
}
}()
// The WaitGroup will track completion of all our workers.
wg := new(sync.WaitGroup)
wg.Add(WorkerCount)
// Divide the work up over the number of workers.
chunkSize := len(data) / WorkerCount
// Spawn workers.
for i := 0; i < WorkerCount; i++ {
go func(i int) {
offset := i * chunkSize
worker(result, data[offset:offset+chunkSize])
wg.Done()
}(i)
}
// Wait for all workers to finish, before returning the result.
wg.Wait()
return sum
}
// worker sums up the numbers in the given list.
func worker(result chan int, data []int) {
var sum int
for _, v := range data {
sum += v
}
result <- sum
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2772 次 |
最近记录: |