←back to thread

149 points whack | 8 comments | | HN request time: 0.001s | source | bottom
1. groundzeros2015 ◴[] No.45780994[source]
Did this article share more than one myth?

The reason why programmers don’t believe in cache coherency is because they have experienced a closely related phenomena, memory reordering. This requires you to use a memory fence when accessing a shared value between multiples cores - as if they needed to synchronize.

replies(2): >>45781211 #>>45781571 #
2. Lwerewolf ◴[] No.45781211[source]
I'm pretty sure that most cases of x86 reordering issues are a matter of the compiler reordering things, which isn't (afaik) solved with just "volatile". Caveat - haven't dealt with this for at least over a year (multicore sync without using OS primitives in general).
replies(2): >>45781334 #>>45781345 #
3. nly ◴[] No.45781334[source]
x86 has a Total Store Order (TSO) memory model, which effectively means (in a mental model where only 1 shared memory operation happens at once and completes before the next) stores are queued but loads can be executed immediately even if stores are queued in the store buffer.

On a single core a load can be served from the store buffer (queue), but other cores can't see those stores yet, which is where all the inconsistencies come from.

4. fer ◴[] No.45781345[source]
Literally the volatile keyword in Java is to make the Java compiler aware in order to insert memory barriers. That only guarantees consistent reads or writes, but it doesn't make it thread safe (i.e. write after read), that's what atomics are for.

Also not only compilers reorder things, most processors nowadays do OoOE; even if the order from the compiler is perfect in theory, different latencies for different instruction operands may lead to execute later things earlier not to stall the CPU.

replies(1): >>45781512 #
5. zozbot234 ◴[] No.45781512{3}[source]
Note that this is only true of the Java/C# volatile keyword. The volatile keyword in C/C++ is solely about direct access in hardware to memory-mapped locations, for such purposes as controlling external devices; it is entirely unrelated to the C11 memory model for concurrency, does not provide the same guarantees, and should never be used for that purpose.
6. igtztorrero ◴[] No.45781571[source]
In Golang there are sync.Mutex, sync.Atomic and Channels to create this fence and prevent data races. I prefer sync.Mutex.

Does anyone understand how Go handles the CPU cache?

replies(1): >>45783554 #
7. groundzeros2015 ◴[] No.45783554[source]
Yes. Locks will use a memory fence. More advanced programs will need fence without locking.
replies(1): >>45784556 #
8. ◴[] No.45784556{3}[source]