dongwei2983 2019-02-11 06:05
浏览 65
已采纳

是否可以可靠地顺序写入两个单独的通道?

If I have a select on two channels:

for {
    select {
    case <-chan1:
        // do something
    case <-chan2:
        // do something else
    }
}

And in a separate goroutine I write sequentially to those channels:

chan1 <- "blah"
chan2 <- true

Am I guaranteed to see "do something" execute before "do something else"?

I know that select chooses randomly if it has two unblocked channels, but I was thinking that writing to the first channel might reliably "interrupt" the writing goroutine if the select is already blocking, implying that the select would run on the unblocked first channel before the second write.

  • 写回答

1条回答 默认 最新

  • driuwt9557 2019-02-11 06:41
    关注

    Yes you can rely on this if the channels are unbuffered. See the Go Memory Model.

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

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100