对于单个指针的可见性,我应该使用同步吗?

huangapple go评论60阅读模式
英文:

Should I use synchronization for visibility of a single pointer?

问题

根据参考文献,它说:

> 否则,对于单字大小或子字大小的内存位置的每次读取,必须观察到实际写入该位置的值(可能是由并发执行的goroutine写入的),并且尚未被覆盖。

由于指针的大小与一个字相同,这是否意味着仅仅为了可见性而不需要同步呢?

英文:

As per the reference, it says

> Otherwise, each read of a single-word-sized or sub-word-sized memory location must observe a value actually written to that location (perhaps by a concurrent executing goroutine) and not yet overwritten.

Since a pointer is word-sized, does that mean synchronization is not necessary for mere purpose of visiblity?

答案1

得分: 1

不,可见性意味着当一个goroutine写入一个变量时,其他goroutine可以看到这个变化。因此,可见性需要同步。否则,一个读取的goroutine可能会看到变量的旧值(未更新)。

英文:

No, visibility means that when one goroutine writes to a variable, other goroutines see the change. So, visibility requires synchronization. Otherwise a reading goroutine will possibly see a stale (not updated) value of the variable.

huangapple
  • 本文由 发表于 2022年10月6日 15:15:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/73969912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定