正在更新双操作原子 [英] Is Updating double operation atomic

查看:114
本文介绍了正在更新双操作原子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,更新double和long变量可能不是原子的,因为double / long被视为两个单独的32位变量。

In Java, updating double and long variable may not be atomic, as double/long are being treated as two separate 32 bits variables.

http://java.sun.com/docs/books/jls/second_edition/html /memory.doc.html#28733

在C ++中,如果我使用32位Intel Processor + Microsoft Visual C ++编译器, )操作原子?

In C++, if I am using 32 bit Intel Processor + Microsoft Visual C++ compiler, is updating double (8 byte) operation atomic?

我不能在这个行为上找到太多的规范。

I cannot find much specification mention on this behavior.

当我说原子变量我的意思是:

When I say "atomic variable", here is what I mean :

线程A试图写1到变量x。
线程B试图写2到变量x。

Thread A trying to write 1 to variable x. Thread B trying to write 2 to variable x.

我们将从变量x得到值1或2,但不是未定义的值。

We shall get value 1 or 2 out from variable x, but not an undefined value.

推荐答案

这是硬件特定的,取决于架构。对于x86和x86_64,如果它们对齐,则8个字节的写或读保证是原子的。引用英特尔架构内存订购白皮书:

This is hardware specific and depends an the architecture. For x86 and x86_64 8 byte writes or reads are guaranteed to be atomic, if they are aligned. Quoting from the Intel Architecture Memory Ordering White Paper:


英特尔64内存排序保证
每个以下
存储器访问指令,
组成存储器操作出现
作为单个存储器访问
执行,而不考虑存储器类型:

Intel 64 memory ordering guarantees that for each of the following memory-access instructions, the constituent memory operation appears to execute as a single memory access regardless of memory type:

读取或写入单个字节的指令。

  1. Instructions that read or write a single byte.

读取或写入字(2字节)

Instructions that read or write a word (2 bytes) whose address is aligned on a 2 byte boundary.

读取或写入地址为
的双字(4字节)的指令

Instructions that read or write a doubleword (4 bytes) whose address is aligned on a 4 byte boundary.

所有锁定的指令(隐式
锁定xchg指令和其他
读取 - 修改 - 写入指令与
锁定前缀)是不可分割的和
不间断的加载序列
后跟存储,而不管
存储器类型和对齐。

All locked instructions (the implicitly locked xchg instruction and other read-modify-write instructions with a lock prefix) are an indivisible and uninterruptible sequence of load(s) followed by store(s) regardless of memory type and alignment.

这篇关于正在更新双操作原子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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