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

unbuffered channel 和只有一个元素的 buffered channel 有啥区别

  •  
  •   ponymaggie · 2022-06-21 10:47:13 +08:00 · 1056 次点击
    这是一个创建于 674 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如:

    sema = make(chan struct{}, 1)

    和 sema = make(chan struct{})

    都是只能放一个元素,读取之前再放的话就会等待

    2 条回复    2022-06-21 15:59:05 +08:00
    iBugOne
        1
    iBugOne  
       2022-06-21 10:50:42 +08:00   ❤️ 1
    unbuffered channel 第一个元素都放不进去吧,会卡到有人读才行

    https://go.dev/play/p/UJGaZDcKzDW
    qianxi0410
        2
    qianxi0410  
       2022-06-21 15:59:05 +08:00
    unbuffered channel 会卡发送者,buffer channel 不卡
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3247 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:20 · PVG 22:20 · LAX 07:20 · JFK 10:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.