douxie1692 2015-07-29 17:23
浏览 30

检查准备就绪的同时通道

I would like to know if the go language allows checking for multiple channels being ready at the same time.

Here is a somewhat contrived example of what I'm trying to do. (The actual reason is to see if I can implement petrinets natively in go)

package main

import "fmt"

func mynet(a, b, c, d <-chan int, res chan<- int) {
    for {
        select {
        case v1, v2 := <-a, <-b:
            res <- v1+v2
        case v1, v2 := <-c, <-d:
            res <- v1-v2
        }
    }
}

func main() {
        a := make(chan int)
        b := make(chan int)
        c := make(chan int)
        d := make(chan int)
        res := make(chan int, 10)
        go mynet(a, b, c, d, res)

        a <- 5
        c <- 5
        d <- 7
        b <- 7
        fmt.Println(<-res)
        fmt.Println(<-res)
}

This doesn't compile as shown. It can be made to compile by only checking one channel, but then it can trivially deadlock if that channel is ready but the other one is not.

package main

import "fmt"

func mynet(a, b, c, d <-chan int, res chan<- int) {
    for {
        select {
        case v1 := <-a:
            v2 := <-b
            res <- v1+v2
        case v1 := <-c:
            v2 := <-d
            res <- v1-v2
        }
    }
}

func main() {
        a := make(chan int)
        b := make(chan int)
        c := make(chan int)
        d := make(chan int)
        res := make(chan int, 10)
        go mynet(a, b, c, d, res)

        a <- 5
        c <- 5
        d <- 7
        //a <- 5
        b <- 7
        fmt.Println(<-res)
        fmt.Println(<-res)
}

In the general case, I might have multiple cases waiting on the same channel, e.g.

case v1, v2 := <-a, <-b:
...
case v1, v2 := <-a, <-c:
...

so I can't commit to either branch when a value is ready on channel a: only when all values are ready.

  • 写回答

2条回答 默认 最新

  • dongsu0308 2015-07-29 17:49
    关注

    You can't select on multiple channels simultaneously. What you can do is implement a fan-in pattern for coalescing your values from multiple channels.

    A rough example based on your code might look like:

    func collect(ret chan []int, chans ...<-chan int) {
        ints := make([]int, len(chans))
        for i, c := range chans {
            ints[i] = <-c
        }
        ret <- ints
    }
    
    func mynet(a, b, c, d <-chan int, res chan<- int) {
        set1 := make(chan []int)
        set2 := make(chan []int)
        go collect(set1, a, b)
        go collect(set2, c, d)
        for {
            select {
            case vs := <-set1:
                res <- vs[0] + vs[1]
            case vs := <-set2:
                res <- vs[0] + vs[1]
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值