atomic.LoadUint32 是必需的吗?

Is atomic.LoadUint32 necessary?

Go 的原子包提供函数func LoadUint32(addr *uint32) (val uint32)。我查看了程序集实现:

TEXT ·LoadUint32(SB),NOSPLIT,[=10=]-12
MOVQ    addr+0(FP), AX
MOVL    0(AX), AX
MOVL    AX, val+8(FP)
RET

基本上从内存地址加载值并 return 它。 我想知道如果我们有一个 uint32 pointer(addr) x,调用 atomic.LoadUint32(x) 和直接使用 *x 访问它有什么区别?

which basically load the value from the memory address and return it.

在您的上下文中就是这种情况,但在 atomicity is to be implemented, as discussed here.
的不同机器架构上可能会有所不同 如go issue 8739

所述

We intrinsify both sync/atomic and runtime/internal/atomic for a bunch of architectures.
The APIs are not unified (e.g. LoadUint32 in sync/atomic is Load in runtime/internal/atomic).

(* “内化”如 issue 4947)

正如我在第一个 link 中提到的:

Regarding loads and stores.

Memory model along with instruction set specifies whether plain loads and stores are atomic or not. Typical guarantee for all modern commodity hardware is that aligned word-sized loads and stores are atomic. For example, on x86 architecture (IA-32 and Intel 64) 1-, 2-, 4-, 8- and 16-byte aligned loads and stores are all atomic (that is, plain MOV instruction, MOVQ and MOVDQA are atomic).