Can multiple CPUs simultaneously write to the same RAM location?

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.

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. 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. 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.

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.

There is nothing that prevents you from doing this on a low level. RAM writes are atomic however, so memory controller will execute 2 seemingly simulateneous writes from cores sequentially.

– T.E.D. Feb 5 '09 at 17:40 AFAIR, it depends on the implementation. Memory contoller is ultimately what decides how to write data to RAM. CPU just issues command to it and provides data via data bus and address via address bus.

– xelurg Feb 5 '09 at 17:46.

So, maybe to the CPU's it is simultaneous, but the RAM controller will determine whose command is processed first.

They shouldn't because the resulting RAM content would be unspecified if different values were written.

In fact it will be the value of the latest data register submitted to the write pipeline in memory controller – xelurg Feb 5 '09 at 17:36 @xelurg: I agree. But in that case the actual writes are not simultaneous (since they have to be pipelined). In other words, it wouldn't make sense to have two independent write pipelines for the same memory.

– Zach Scrivena Feb 5 '09 at 17:46.

Awrynowicz Feb 5 '09 at 17:41 I think you're referring to the micro-code, which is set of signal levels applied to elements within a CPU... opcode is a higher level - it can operate on various set of elements, including memory and I/O port. En.wikipedia.org/wiki/Opcode – xelurg Feb 5 '09 at 17:42 forgot to include a link to microcode blurb :) en.wikipedia.org/wiki/Microcode – xelurg Feb 5 '09 at 17:47 yabcok: I meant ChrisW is referring to microcode when saying that opcode is more likely to write to cache rather then RAM – xelurg Feb 5 '09 at 17:50 "Aren't CPU caches write-through? " Googling suggests that writes can be "write-back" instead of "write-through", and that some (Intel) CPUs use a hybrid "write-back" and "write-through".

Opcodes aren't as 1-to-1 as they used to be with what's happening at the hardware level. – ChrisW Feb 5 '09 at 17:57.

They can try, but hardware will be the ultimate determinant of what happens.

OK. How about core2 duo? – Jacek?

Awrynowicz Feb 5 '09 at 18:04 It's still up to the hardware, and by hardware I mean how the CPU interfaces with memory. – MSN Feb 5 '09 at 19:44.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions