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 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式