本文介绍了变量赋值是原子的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个线程同时修改结构上的字符串字段,我将始终看到分配给该字段的一个或另一个字符串吗?

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?

推荐答案

否。如果您需要原子操作,则有。

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:

要序列化访问,请使用通道操作或其他
同步原语(例如 sync sync / atomic
软件包。

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

这篇关于变量赋值是原子的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 17:43
查看更多