如何自动读取x86 ASM中的值? [英] How do I atomically read a value in x86 ASM?

查看:33
本文介绍了如何自动读取x86 ASM中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在x86 ASM中自动写入一个值.但是,我该怎么读? LOCK前缀不能与mov一起使用.

I know how to atomically write a value in x86 ASM. But how do I read one? The LOCK prefix can't be used with mov.

要增加价值,我正在做

lock inc dword ptr Counter

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

How do I read Counter in a thread-safe way?

推荐答案

我不是汇编专家,但是字大小(在x86,32位上)的读/写应该已经是原子的.

I'm not an assembly expert, but word-sized (on x86, 32-bit) reads/writes should be atomic already.

您需要锁定增量的原因是因为这既是读取又是写入.

The reason you need to lock the increment is because that's both a read AND a write.

这篇关于如何自动读取x86 ASM中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆