dqt83336 2013-08-02 07:42
浏览 22
已采纳

去引导两个以上的goroutines

One thing I do not like about Go is that channel receive also removes the data from the channel. This only allows two goroutines to communicate with each other even though there are several cases when two or more goroutines should be able to talk to each other.

I know I could make an array of channels and have channel per goroutine, but moving data from one goroutine to all of the other goroutines is much more data on ram than moving one copy of the data to all goroutines.

Think about a case when I have thousand clients connected to server and I want one to send message to only half of them that is five hundred goroutines receiving that message. If the message is 512 bytes this becomes 250 kilobytes of data in the ram moving, even though it's possible to move the same data only once if channels would not remove the data on receive.

So I am asking if there is some simple way to do this or do I have to use mutex from the sync package? Though please tell me if I am calculating wrong and channels do not copy the data, because in that case I can just manage array of channels.

  • 写回答

2条回答 默认 最新

  • dozrhldy285751 2013-08-02 08:02
    关注

    Read this article:

    http://rogpeppe.wordpress.com/2009/12/01/concurrent-idioms-1-broadcasting-values-in-go-with-linked-channels/

    It's an analysis on the different ways to channel-broadcast across goroutines, one of them being particularly interesting:

    type Broadcaster struct {
        Listenc chan chan (chan broadcast);
        Sendc   chan<- interface{};
    }
    

    This approach is called a "linked channel" (analagously to a linked list) by the author.

    tell me if I am calculating wrong and channels do not copy the data, because in that case I can just manage array of channels.

    You're not wrong. As @Jsor suggested, though - you can just pass pointers around if you're afraid of copy-overhead and the use case allows it.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?