如何关闭多个goroutines正在发送的频道?

时间:2013-03-30 04:08:28

标签: go channels

我试图并行进行一些计算。该程序的设计是为了让每个工作者goroutine将已解决的谜题的“碎片”发送回控制器goroutine,等待接收和组装从工作程序发送的所有内容。

什么是关闭单一频道的idomatic Go?我不能在每个goroutine的频道上打电话,因为那时我可以发送一个封闭的频道。同样,没有办法预先确定哪个goroutine会先完成。这里是否需要sync.WaitGroup?

2 个答案:

答案 0 :(得分:9)

以下是使用sync.WaitGroup执行您要找的内容的示例

这个例子接受一个长整的整数列表,然后通过将N个并行工作者交给一个相等大小的输入数据块来对它们进行求和。它可以在go playground上运行:

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
}

答案 1 :(得分:4)

是的,这是sync.WaitGroup的完美用例。

您的另一个选择是使用每个goroutine 1个通道和一个多路复用器goroutine,从每个通道馈送到单个通道。但这会很快变得笨拙,所以我只选择sync.WaitGroup。