为了账号安全,请及时绑定邮箱和手机立即绑定

两个包之间的通道通信死锁 - Golang

两个包之间的通道通信死锁 - Golang

Go
繁星点点滴滴 2022-06-27 17:20:56
两个包之间的通道通信死锁 - Golang。我有两个包通过两个渠道进行通信。一个是主要的,另一个是功能。当我运行它时,我会遇到死锁。package mainimport (    functionspackage "GoEjemplos/subFolder"    "fmt"    "sync")func main() {    var wg sync.WaitGroup    ChannelSendData := make(chan functionspackage.FunctionStruct, 1)    defer close(ChannelSendData)    data := functionspackage.FunctionStruct{        FieldOne: 3.56,        FieldTwo: 23,    }    ChannelSendData <- data    wg.Add(1)    go functionspackage.FunctionExt(ChannelSendData, &wg)    recibe := <-functionspackage.ChannelOutFunct    fmt.Println("channelOut: ", recibe)    close(functionspackage.ChannelOutFunct)    wg.Wait()}另一个包是package functionspackageimport "sync"type FunctionStruct struct {    FieldOne float64    FieldTwo int}var ChannelOutFunct chan float64func FunctionExt(RecibeChan chan FunctionStruct, wg *sync.WaitGroup) (ChannelOutFunct chan float64) {    reciveData := <-RecibeChan    result := reciveData.FieldOne * float64(reciveData.FieldTwo)    ChannelOutFunct <- result    wg.Done()    return ChannelOutFunct}这就是僵局。PS C:\Go-Project\src\GoEjemplos> go run main.gofatal error: all goroutines are asleep - deadlock!goroutine 1 [chan receive (nil chan)]:main.main()        C:/Go-Project/src/GoEjemplos/main.go:32 +0x13dgoroutine 19 [chan send (nil chan)]:GoEjemplos/subFolder.FunctionExt(0xc0000d4000, 0xc0000a2070, 0xc0000c9f18)        C:/Go-Project/src/GoEjemplos/subFolder/functionsPackage.go:19 +0x85created by main.main        C:/Go-Project/src/GoEjemplos/main.go:30 +0x11aexit status 2PS C:\Go-Project\src\GoEjemplos>你能给我解释一下问题出在哪里吗?
查看完整描述

2 回答

?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

通道functionspackage.ChannelOutFunct未初始化,因此它是一个零通道。写入零通道或从零通道读取将始终阻塞。

https://dave.cheney.net/2014/03/19/channel-axioms


查看完整回答
反对 回复 2022-06-27
?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

我让它工作。这是代码


package main


import (

    packagefunctions "GoEjemplos/subFolder"

    "fmt"

    "sync"

)


func main() {

    var wg sync.WaitGroup

    var ChannelSendData = make(chan packagefunctions.FunctionStruct, 0)

    defer close(ChannelSendData)

    var ChanReturn = make(chan float64)

    defer close(ChanReturn)


    data := packagefunctions.FunctionStruct{

        FieldOne: 3.56,

        FieldTwo: 2,

    }


    wg.Add(1)

    go func() { ChannelSendData <- data }()


    wg.Add(1)

    go func() {

        ChanReturn = packagefunctions.FunctionExt(ChannelSendData, &wg)

        recibeChanReturn := <-ChanReturn

        fmt.Println("channelOut: ", recibeChanReturn)

        wg.Done()

    }()

    wg.Wait()

}

另一个包是


package packagefunctions


import (

    "fmt"

    "sync"

)


type FunctionStruct struct {

    FieldOne float64

    FieldTwo int

}


func FunctionExt(ChanIn chan FunctionStruct, wg *sync.WaitGroup) chan float64 {

    reciveData, ok := <-ChanIn

    if ok == false {

        fmt.Println("channel closed")

    }

    var result float64

    result = reciveData.FieldOne * float64(reciveData.FieldTwo)

    var ChannelReturn = make(chan float64, 1)

    defer close(ChannelReturn)

    ChannelReturn <- result

    wg.Done()

    return ChannelReturn

}


查看完整回答
反对 回复 2022-06-27
  • 2 回答
  • 0 关注
  • 103 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信