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 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备