在我看来,术语 read-modify-write 没有多大意义,因为 modify 只是说 write 的另一种方式>。引自wikipedia(强调我的):
在计算机科学中,read-modify-write是一类原子操作 [...] 都读取内存位置并写入新值 它同时 [...]
那为什么不简单地读写呢?两者之间有任何微妙的语义差异吗?
答案 0 :(得分:3)
为什么它被称为读 - 修改 - 写但不是读写?
因为这正是典型架构上的事件序列,例如Mat magn;
magnitude(planes[0], planes[1], magn);
for (int i = 0; i < width; i++)
result[i] = pow(magn.at<float>(0,i),2);
。
为了创建原子性感知,缓存行在X86
和read
操作之间被锁定。
例如,递增C ++原子变量:
write
由g.fetch_add(1);
编译成:
gcc
尽管只是一条指令,0x00000000004006c0 <+0>: lock addl $0x1,0x200978(%rip) # 0x601040 <g>
本身并不是原子的。
addl
前缀是必要的,以保证更新的寄存器值在被其他内核访问之前写回缓存行
(存储缓冲区已刷新,但为RMW操作被绕过)。
lock
缓存一致性协议确保所有内核在释放缓存行锁定后观察更新的内存值。
这可以保证所有线程都遵循C ++标准对RMW操作所需的修改顺序中的最新值。
答案 1 :(得分:0)
The way you ask the question is out of context with the way I would expect it in normal usage. In looking up the link the topic is about atomic operations.
Atomic operations are guaranteed to perform the entire action without getting interrupted which is very important.
Normally when programmers talk about reading and writing
a value in assembly they mean that one instructions is used to read the value from memory into a register and another instruction is used to write a register, or immediate value to a memory location. Also when they talk about this it can but does not mean that they are talking about the same value being read from a memory location, updated, and written back to the same location as two back to back instructions. Usually the read and write instructions for a memory location are a few instructions apart.
Also the way a value can be changed with an atomic instruction is very limited if you look at the examples given, e.g.
Set a location to 1
Increment by a set value
Swap a location with a set value
These operations try to keep the clock cycles down as they lock out interrupts and such so that they remain atomic.
If I were speaking with another programmer I would probably use the term atomic operation instead of read-modify-write. I actually had to read the link to realize it was about atomic operations. But then again I have been programming for about four decades and common phrases do change.