V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
koujianshusheng
V2EX  ›  Go 编程语言

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

  •  
  •   koujianshusheng · 2022-05-27 17:51:12 +08:00 · 1498 次点击
    这是一个创建于 692 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2022-05-27 19:11:08 +08:00
    koujianshusheng
        1
    koujianshusheng  
    OP
       2022-05-27 17:52:02 +08:00
    一个堵塞一个死锁,就因为 net 包?
    youngzy
        2
    youngzy  
       2022-05-27 18:10:51 +08:00
    第一个应该也是死锁,没有创建新的协程,主协程阻塞。
    去 play ground 试试?
    koujianshusheng
        3
    koujianshusheng  
    OP
       2022-05-27 18:40:29 +08:00
    @youngzy 我是在本地环境运行的,play ground 会死锁,但是本地没有
    codehz
        4
    codehz  
       2022-05-27 18:43:39 +08:00 via Android
    引用 cgo 的包(包括 net )就会关闭死锁检测器
    koujianshusheng
        5
    koujianshusheng  
    OP
       2022-05-27 18:47:11 +08:00
    @codehz 这个说法有参考资料吗,亲
    iBugOne
        6
    iBugOne  
       2022-05-27 18:54:21 +08:00 via Android
    koujianshusheng
        7
    koujianshusheng  
    OP
       2022-05-27 18:56:17 +08:00
    @iBugOne 感谢
    lysS
        8
    lysS  
       2022-05-27 19:00:54 +08:00
    无论是否 import net ,我这里都是 17 没有报错,18 报错
    lysS
        9
    lysS  
       2022-05-27 19:03:01 +08:00
    知道了,net 没有 cgo ,可以 import "C"来检测
    zuokanyunqishi
        10
    zuokanyunqishi  
       2022-05-27 19:11:08 +08:00
    go1.18.1 复现。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5261 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 08:40 · PVG 16:40 · LAX 01:40 · JFK 04:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.