Skip to main content
GameDev.net gamedev.net
🔒 Locked

Processor Self-Consistency

Started by Catafriggm Jan 1, 2007 at 2:01 PM 2 replies 750+ views
Original Post
Catafriggm
Catafriggm
Processors typically guarantee self-consistency. My question is whether self-consistency in and of itself guarantees that a read performed before a write to the same address will see the value of the memory location before the write. The case I'm dealing with takes this one step further by adding a conditional jump in between. Something like this: if (var == something) var = somethingelse; Yes, right? Just need to make sure.
jbarcz1
jbarcz1

You can always count on reads and writes in the same thread being resolved in the correct order. You have no guarantees if multiple threads are accessing the same memory.
Joshua Barczak3D Application Research GroupAMD
Excors
Excors
I think it might be more accurate to say that you can count on reads and writes appearing to be resolved in the correct order as long as you only have one thread accessing that memory. Optimising compilers reorder everything, without changing the appearance of what order things happen in, working on the assumption that it's not their job to worry about threading (because it's not at all defined by the language specification, unlike e.g. Java) - so reads and writes will really happen out of order or not at all (e.g. if they're only stored in registers), unless you tell the compiler to be careful.

"volatile" tells the compiler to be careful, and MSVC has things like _ReadWriteBarrier:
Quote:
The Visual C++ compiler is free to perform any optimization that preserves the meaningful outputs of the program in single-threaded execution. These barriers are provided to block optimization of reads and writes at specific points in a program. This is similar to marking that memory "volatile" only more performant (when usable) because it only forces reads and writes to complete at specific points in a program, rather than globally.

so it's good to use something like that if you do have multiple threads and the order of operations matters.

But if you only have a single thread, there's no need to worry since it's guaranteed to work consistently the same as if everything was done in the simple logical order.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.