英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论