dongsu1539 2017-10-04 04:07
浏览 887
已采纳

Golang atomic.LoadUint32是否必要?

Golang's atomic package provides function func LoadUint32(addr *uint32) (val uint32). I looked into the assembly implementation:

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

which basically load the value from the memory address and return it. I'm wondering if we have a uint32 pointer(addr) x, what is the difference between calling atomic.LoadUint32(x) and directly access it using *x?

  • 写回答

1条回答 默认 最新

  • dongxi2014 2017-10-04 04:34
    关注

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

    That is the case in your context, but might differ on a different machine architecture where atomicity is to be implemented, as discussed here.
    As mentioned in 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).

    (* "intrinsify" as in issue 4947)

    As mentioned in my first 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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端