登录
首页 >  Golang >  Go问答

在管道中传递通道作为参数时出现死锁

来源:stackoverflow

时间:2024-04-09 22:18:35 472浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《在管道中传递通道作为参数时出现死锁》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

问题内容

我正在编写一种实践,将数字分为 100 组并同时计算阶乘,但是我的代码给我带来了死锁。

我认为问题可能是管道链的启动。由于所有函数都以通道作为参数,所以我不清楚为什么当此行 total:= <- c 发生时,main 中的 go func 不将给定的 genconcurrentgroup 通道值传递给 genconcurrentgroup 函数。

package main

import "fmt"

func main() {
    in := make (chan int)
    out := make (chan float64)

    go func() {
        in <- 1005
        out = calculateFacotorial(genConcurrentGroup(in))
    }()

    fmt.Println(<-in)
    fmt.Println(<-out)

}

//split input number into groups
//the result should be a map of [start number, number in group]
//this is not heavy task so run in one go routine
func genConcurrentGroup(c chan int) chan map[int]int{
    out := make(chan map[int]int)

    go func() {
        //100 groups
        total:= <- c  //DEADLOCK HERE! Why?
        //element number in group
        elemNumber := total / 100
        extra := total % 100
        result := make(map[int]int)
        if elemNumber>0{
            //certain 100 groups
            for i:=1 ;i<=99;i++{
                result[(i-1) * elemNumber + 1] = elemNumber
            }
            result[100] = extra + elemNumber
        }else{
            //less than 100
            for i:=1;i<=total;i++{
                result[i] = 1
            }
        }

        out <- result
        close(out)
    }()
    return out
}

//takes in all numbers to calculate multiply result
//this could be heavy so can do it 100 groups together
func calculateFacotorial(nums chan map[int]int) chan float64{
    out := make(chan float64)
    total:= <- nums //DEADLOCK HERE! Why?

    go func() {

        oneResult := make(chan float64)

        for k,v := range total{
            go func() {
                t := 1.0
                for i:=0;i<v;i++{
                    t *= float64(k) + float64(i)
                }
                oneResult <- t
            }()
        }
        result := 1.0
        for n := range oneResult{
            result *= n
        }

        close(oneResult)
    }()
    return out
}

解决方案


1。 total:= <-c //这里死锁!为什么?

您需要向 in 通道添加缓冲区,因为当您发送值时没有人接收。

2。 total := <-nums //这里死锁!为什么?

您需要添加一个 sync.waitgroup 来等待所有 goroutine 结束,然后您可以关闭通道以循环它。

没有死锁的完整代码:

package main

import (
    "fmt"
    "sync"
)

func main() {
    in := make(chan int, 1)

    in <- 1005
    out := calculateFacotorial(genConcurrentGroup(in))

    fmt.Println(<-out)

}

//split input number into groups
//the result should be a map of [start number, number in group]
//this is not heavy task so run in one go routine
func genConcurrentGroup(c chan int) chan map[int]int {
    out := make(chan map[int]int)

    go func() {
        //100 groups
        total := <-c //DEADLOCK HERE! Why?
        //element number in group
        elemNumber := total / 100
        extra := total % 100
        result := make(map[int]int)
        if elemNumber > 0 {
            //certain 100 groups
            for i := 1; i <= 99; i++ {
                result[(i-1)*elemNumber+1] = elemNumber
            }
            result[100] = extra + elemNumber
        } else {
            //less than 100
            for i := 1; i <= total; i++ {
                result[i] = 1
            }
        }

        out <- result
        close(out)
    }()
    return out
}

//takes in all numbers to calculate multiply result
//this could be heavy so can do it 100 groups together
func calculateFacotorial(nums chan map[int]int) chan float64 {
    out := make(chan float64)
    total := <-nums //DEADLOCK HERE! Why?

    go func() {
        oneResult := make(chan float64, len(total))

        var wg sync.WaitGroup
        wg.Add(len(total))

        for k, v := range total {
            go func() {
                t := 1.0
                for i := 0; i < v; i++ {
                    t *= float64(k) + float64(i)
                }
                oneResult <- t
                wg.Done()
            }()
        }

        wg.Wait()

        close(oneResult)

        result := 1.0
        for n := range oneResult {
            result *= n
        }

        out <- result

    }()
    return out
}

今天关于《在管道中传递通道作为参数时出现死锁》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>