The Go Programming Language
› http://golang.org/
› Go Playground
Go Projects
› Revel Web Framework
koujianshusheng

关于 go 无缓冲 channel 的 deadlock 问题!

  •  
  •   koujianshusheng · May 27, 2022 · 2938 views
    This topic created in 1621 days ago, the information mentioned may be changed or developed.

    package main

    import ( "fmt" "net" )

    var temp net.IP

    func main() { fmt.Println("start......") ch := make(chan int) <-ch fmt.Println("hello world") }

    /* start......

    */

    package main

    import ( "fmt" )

    func main() { fmt.Println("start......") ch := make(chan int) <-ch fmt.Println("hello world") }

    /* start...... fatal error: all goroutines are asleep - deadlock!

    goroutine 1 [chan receive]: main.main() */

    10 replies  •  2022-05-27 19:11:08 +08:00
    koujianshusheng
    koujianshusheng
    OP
    May 27, 2022
    1
    一个堵塞一个死锁,就因为 net 包?
    youngzy
    youngzy
    May 27, 2022
    2
    第一个应该也是死锁,没有创建新的协程,主协程阻塞。
    去 play ground 试试?
    koujianshusheng
    koujianshusheng
    OP
    May 27, 2022
    3
    @youngzy 我是在本地环境运行的,play ground 会死锁,但是本地没有
    codehz
    codehz
    May 27, 2022 via Android
    4
    引用 cgo 的包(包括 net )就会关闭死锁检测器
    koujianshusheng
    koujianshusheng
    OP
    May 27, 2022
    5
    @codehz 这个说法有参考资料吗,亲
    koujianshusheng
    koujianshusheng
    OP
    May 27, 2022
    7
    @iBugOne 感谢
    lysS
    lysS
    May 27, 2022
    8
    无论是否 import net ,我这里都是 17 没有报错,18 报错
    lysS
    lysS
    May 27, 2022
    9
    知道了,net 没有 cgo ,可以 import "C"来检测
    zuokanyunqishi
    zuokanyunqishi
    May 27, 2022
    10
    go1.18.1 复现。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4946 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 06:43 · PVG 14:43 · LAX 22:43 · JFK 01:43
    ♥ Do have faith in what you're doing.