Is variable assignment atomic in go?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:33:23

问题


If I have two threads concurrently modifying a string field on a struct, will I always see one or the other string assigned to the field, but nothing else?


回答1:


No. If you need atomic operations, there is sync/atomic.

The Go Memory Model will have all the related details. From the top of the Memory Model document:

Programs that modify data being simultaneously accessed by multiple goroutines must serialize such access.

To serialize access, protect the data with channel operations or other synchronization primitives such as those in the sync and sync/atomic packages.



来源:https://stackoverflow.com/questions/34750323/is-variable-assignment-atomic-in-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!