duanchao4445 2012-12-15 18:54
浏览 98
已采纳

go使用共享内存还是分布式计算

Go has the slogan "Do not communicate by sharing memory; instead, share memory by communicating". I was wondering whether Go uses shared memory or distributed computing approach. For example, for MPI it is clearly distributed, OpenMP is clearly shared memory; but I was not sure about Go, which is unique.

I have seen many posts, such as Shared memory vs. Go channel communication, effective Go document etc., but could not clarify. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • doupuchen6378 2012-12-15 22:30
    关注

    Go does not prevent you from sharing memory between goroutines/threads. What they mean by communicating, is that you send a chunk of data, or a pointer to said chunk, across a channel. This effectively transfers 'ownership' of the data to the target reader of the channel. Mind you, this transfer of ownership is not enforced by the language or the runtime, it is just by convention.

    You are still perfectly capable of writing to the same memory from two goroutines, if you so choose. In other words: Go does not prevent you from shooting yourself in the foot, it just provides language semantics which make these mistakes easier to detect.

    If a value is passed into a channel, the programmer must then assume that value is no longer his to write to in the same goroutine.

    func F(c chan *T) {
        // Create/load some data.
        data := getSomeData()
    
        // Send data into the channel.
        c <- data
    
        // 'data' should now be considered out-of-bounds for the remainder of
        // this function. This is purely by convention, and is not enforced
        // anywhere. For example, the following is still valid Go code, but will
        // lead to problems.
        data.Field = 123
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流