duanboniao5903 2018-12-11 21:32
浏览 85
已采纳

为什么即使死循环中包含一个死循环,我还是会有死锁?

So I've started messing around with Go, and I'm fascinated by go routines. I now wrote a simple test to see if I can change the value of a variable while I continuously print it out.

I now have the following code:

package main

import (
    "fmt"
    "time"
)

func change(c chan float64) float64 {
    time.Sleep(2 * time.Second)
    return 2.5
}

func main() {

    s := 1.1

    c := make(chan float64)
    go change(c)
    s = <-c

    for {
        fmt.Println(s)
        time.Sleep(100 * time.Millisecond)
    }    
}

Unfortunately it ends in an error:

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
        /home/kramer65/repos/go/src/messing_around/main.go:19 +0x7d
exit status 2

I searched around and found that this deadlock happens when the main function ends while goroutines didn't end yet. But since I have an endless loop I wouldn't know what else is wrong with my code.

Could anybody enlighten me as to what is wrong with this code, and how I can change the value of a variable while I continuously print it out? All tips are welcome!

  • 写回答

1条回答 默认 最新

  • duanlu1279 2018-12-11 22:25
    关注

    It looks like you might have a misunderstanding on channels and go routines.

    The line:

    go change(c)
    

    seems to indicate that the function change is going to write to c. However it ends up just returning a value after a period of time.

    This value (2.5) doesn't get received anywhere. Also, c doesn't get written to anywhere. I suspect you intended the 2.5 to be written to the channel c. The syntax for that is as follows:

    c<-2.5
    

    Therefore, if you change your change function to:

    func change(c chan float64) {
        time.Sleep(2 * time.Second)
        c <- 2.5
    }
    

    You should not see the deadlock anymore. Notice I am not returning a float64 anymore.

    I made a playground to ensure this: https://play.golang.org/p/SgLiUmPpcAZ

    Update for Comment

    The 1.1 will always be overwritten by the value of the channel. If however you want to print s's initial value (as stated in the comment), you will have to change the flow a bit and use a select statement:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func change(c chan float64) {
        time.Sleep(2 * time.Second)
        c <- 2.5
    }
    
    func main() {
    
        s := 1.1
    
        c := make(chan float64)
        go change(c)
    
        for {
            select {
            case s = <-c:
            default:
                // c isn't ready yet
            }
            fmt.Println(s)
            time.Sleep(100 * time.Millisecond)
        }
    }
    

    Now that you have a select statement, you can use it with a time.Ticker as well:

    ticker := time.NewTicker(100 * time.Millisecond)
    for {
        select {
        case s = <-c:
        case <-ticker.C:
            fmt.Println(s)
        default:
            // c isn't ready yet
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能