多个 CPU 可以同时写入同一个 RAM 位置吗? [英] Can multiple CPUs simultaneously write to the same RAM location?

查看:39
本文介绍了多个 CPU 可以同时写入同一个 RAM 位置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

机器字大小(或更小)写入序列化了吗?只需一个本机操作码即可将寄存器内容复制到 RAM.

Are machine word size (or smaller) writes serialized? Only one native opcode is needed to copy register content to RAM.

推荐答案

将数据写入 RAM 是原子的.如果两个 CPU 尝试同时写入同一位置,则内存控制器将决定写入的某个顺序.当一个 CPU 正在写入内存时,另一个 CPU 将根据需要停止尽可能多的周期,直到第一次写入完成;然后它会覆盖它的值.这就是所谓的竞争条件.

Writing data to RAM is atomic. If two CPUs try to write to the same location at the same time, the memory controller will decide on some order for the writes. While one CPU is writing to memory, the other CPU will stall for as many cycles as necessary until the first write is completed; then it will overwrite its value. This is what's known as a race condition.

小于本机字大小的写入不是原子的——在这种情况下,CPU 必须将旧的内存值读入寄存器,将新字节写入寄存器,然后将该新值写回内存.

Writes that are smaller than the native word size are not atomic -- in that case, the CPU must read the old memory value into a register, write the new bytes into the register, and then write that new value back to memory.

你永远不应该有依赖于此的代码——如果你有多个 CPU 试图同时写入同一个内存位置,你就做错了.

You should never have code that depends on this -- if you have multiple CPUs that are trying to simultaneously write to the same memory location, you're doing something wrong.

另一个重要的考虑因素是缓存一致性问题.每个 CPU 都有自己的缓存.如果一个 CPU 将数据写入其缓存,则其他 CPU 需要知道该数据值的更改才能读取该数据.

Another important consideration is the cache coherency problem. Each CPU has its own cache. If a CPU writes data to its cache, the other CPUs need to be made aware of the change to that data value if they want to read it.

这篇关于多个 CPU 可以同时写入同一个 RAM 位置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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