dongnunai3125 2018-11-28 01:45
浏览 11
已采纳

我可以基于发送顺序来选择唤醒的顺序吗?

Imagine a code pattern like the following:

var a chan T = make(chan T)
var b chan Q = make(chan Q)

//consumer goroutine
for {
   select {
   case <-a:
       //...
   case <-b:
       //...
   }
}

//producer goroutine
...
a <- x
b <- y
...

Is it guaranteed that we can always retrieve x from case: <-a BEFORE y from case :<-b provided that there is only one producer?

  • 写回答

1条回答 默认 最新

  • dqnhfbc3738 2018-11-28 01:52
    关注

    Yes, you can rely on the order because channel a is unbuffered.

    Communication on an unbuffered channel succeeds only when the sender and receiver are ready. The sender does not execute past the statement a <- x until the value was received. It follows from this that the values are received in send order.

    The order is not guaranteed if channel a is buffered. The receiver might not execute until both values are sent. When the receiver does execute after the values are sent, there's no guarantee on which branch of the select will execute first.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?