gpt4 book ai didi

assembly - 如何以原子方式读取 x86 ASM 中的值?

转载 作者:行者123 更新时间:2023-12-04 13:55:22 25 4
gpt4 key购买 nike

我知道如何在 x86 ASM 中以原子方式写入值。但是我怎么读呢? LOCK 前缀不能与 mov 一起使用。

为了增加值(value),我正在做:

lock inc dword ptr Counter

如何以线程安全的方式读取 Counter?

最佳答案

正如我在 this 中向您解释的那样邮政:

Accesses to cacheable memory that are split across bus widths, cache lines, and page boundaries are not guaranteed to be atomic by the Intel Core 2 Duo, Intel Core Duo, Pentium M, Pentium 4, Intel Xeon, P6 family, Pentium, and Intel486 processors. The Intel Core 2 Duo, Intel Core Duo, Pentium M, Pentium 4, Intel Xeon, and P6 family processors provide bus control signals that permit external memory subsystems to make split accesses atomic; however, nonaligned data accesses will seriously impact the performance of the processor and should be avoided.



所以使用:
LOCK        CMPXCHG   EAX, [J]

LOCK CMPXCHG 首先隔离缓存,然后将 EAX 与目标值进行比较,如果目标值不相等,则 EAX 中的结果为目标值。

编辑:
链接到:

Intel® 64 and IA-32 Architectures Software Developer’s Manuals

Volume 3A: System Programming Guide检查第 8.1.1 节

同时检查: Optimization Reference Manual section: CHAPTER 7 OPTIMIZING CACHE USAGE

关于assembly - 如何以原子方式读取 x86 ASM 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3349859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com