drccfl9407 2016-04-03 21:28
浏览 149
已采纳

说明:不要通过共享内存进行通信; 通过通信共享内存

I wonder what is the most down to earth explanation of this famous quote:

Don't communicate by sharing memory; share memory by communicating. (R. Pike)

In The Go Memory Model I can read this:

A send on a channel happens before the corresponding receive from that channel completes. (Golang Spec)

There is also a dedicated golang article explaining the quote. And key contribution is a working example also by Andrew G.

Well. Sometimes too much talking around .... I have derived from the Memory Spec quotation and also by looking at the working example this:

After a goroutine1 sends (anything) to a goroutine2 via a channel, then all changes (anywhere in the memory) done by goroutine1 must be visible to goroutine2 after it received via same channel. (Golang Lemma by Me:)

Therfore I derive the Down to earth explanation of the famous quote:

To synchronize memory access between two goroutines, you don't need to send that memory via channel. Good enough is to receive from the channel (even nothing). You will see any changes that were written (anywhere) by the goroutine sending (to the channel) at the time of it's send. (Of course, assuming no other goroutines are writing to the same memory.) Update (2) 8-26-2017

I have actually two questions:

1) Is my conclusion correct?

2) Does my explanation help?

Update (1) I am assuming unbuffered channels. Lets restrict ourselves to that first to avoid overhauling ourselves with too many unknowns.

Please, let's also focus on a simple usecase of two goroutines communicating over a single channel and related memory effects and not on the best practices - that is beyond the scope of this question.

To better understand scope of my question assume that the goroutines may access any kind of memory structure - not only primitve ones - and it can be a large one, it can be a string, map, array, whatever.

  • 写回答

6条回答 默认 最新

  • dongni3854 2016-04-03 22:34
    关注

    In essence, yes. Any values that are assigned to variables before the channel send are eligible to be observed after the channel read, since the channel operation imposes an ordering constraint. But it's important to remember the other part of the equation: if you want to guarantee that those values are observed, you have to make sure that no one else can write to those variables in between the write and the read. Obviously using locks would be possible, but pointless at the same time because if you're already combining locks and cross-thread memory modification, what benefit are you getting from channels? You could pass around something as simple as a boolean, as a token allowing exclusive access to the global data, and it would be 100% correct in terms of the memory model guarantees (as long as your code didn't have bugs), it would just probably be a bad design because you would be making things all implicit and action-at-a-distance-y without a good reason; passing the data explicitly is usually going to be clearer and less error-prone.

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

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏