加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

是golang atomic.LoadUint32必要吗?

发布时间:2020-12-16 09:24:57 所属栏目:大数据 来源:网络整理
导读:Golang的原子包提供了函数func LoadUint32(addr * uint32)(val uint32).我查看了程序集实现: TEXT ·LoadUint32(SB),NOSPLIT,$0-12MOVQ addr+0(FP),AXMOVL 0(AX),AXMOVL AX,val+8(FP)RET 它基本上从内存地址加载值并返回它. 我想知道我们是否有一个uint32指
Golang的原子包提供了函数func LoadUint32(addr * uint32)(val uint32).我查看了程序集实现:

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

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

解决方法

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

在您的上下文中就是这种情况,但在要实现atomicity的不同机器架构上可能会有所不同,如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. 07003 in 07004 is 07005 in 07006).

(*“内化”,如issue 4947)

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

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).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读