普通网友 2018-11-17 04:25
浏览 314
已采纳

Golang中通道缓冲容量0和1之间的差异

I have set channel buffer size to zero, like var intChannelZero = make(chan int) , when getting value from the intChannelZero will be blocked until the intChannelZero has value.

Also, I set channel buffer size to one, like var intChannelOne = make(chan int, 1), when getting value from the intChannelOne will be blocked until the intChannelOne has value.

We know the capacity of intChannelZero is zero, the capacity of intChannelOne is one, so I want to know:

  • When putting a value to the intChannelZero like intChannelZero <- 1, where the value be saved?
  • The differences between intChannelZero and intChannelOne when putting a value to them.

Who can explain it at the level of Golang Runtime Enviroment? Thanks a lot.

  • 写回答

2条回答 默认 最新

  • doujing8435 2018-11-17 06:27
    关注

    If the channel is unbuffered (capacity is zero), then communication succeeds only when the sender and receiver are both ready.

    If the channel is buffered (capacity >= 1), then send succeeds without blocking if the channel is not full and receive succeeds without blocking if the buffer is not empty.

    When putting a value to the intChannelZero like intChannelZero <- 1, where the value be saved?

    The value is copied from the sender to the receiver. The value is not saved anywhere other than whatever temporary variables the implementation might use.

    The differences between intChannelZero and intChannelOne when putting a value to them.

    Send on intChannelZero blocks until a receiver is ready.

    Send on intChannelOne blocks until space is available in the buffer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b