Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>volatile</code> only forces your code to re-read the value, it cannot control where the value is read from. If the value was recently read by your code then it will probably be in cache, in which case volatile will force it to be re-read from cache, NOT from memory.</p> <p>There are not a lot of cache coherency instructions in x86. There are prefetch instructions like <a href="http://www.felixcloutier.com/x86/PREFETCHh.html" rel="nofollow noreferrer"><code>prefetchnta</code></a>, but that doesn't affect the memory-ordering semantics. It used to be implemented by bringing the value to L1 cache without polluting L2, but things are more complicated for modern Intel designs with a large shared <em>inclusive</em> L3 cache.</p> <p>x86 CPUs use a variation on the <a href="https://en.wikipedia.org/wiki/MESI_protocol" rel="nofollow noreferrer">MESI protocol</a> (MESIF for Intel, MOESI for AMD) to keep their caches coherent with each other (including the private L1 caches of different cores). A core that wants to write a cache line has to force other cores to invalidate their copy of it before it can change its own copy from Shared to Modified state.</p> <hr> <p>You don't need any fence instructions (like MFENCE) to produce data in one thread and consume it in another on x86, because x86 loads/stores have <a href="http://preshing.com/20120913/acquire-and-release-semantics/" rel="nofollow noreferrer">acquire/release semantics</a> built-in. You do need MFENCE (full barrier) to get sequential consistency. (A previous version of this answer suggested that <code>clflush</code> was needed, which is incorrect).</p> <p>You do need to prevent <a href="http://preshing.com/20120625/memory-ordering-at-compile-time/" rel="nofollow noreferrer">compile-time reordering</a>, because C++'s memory model is weakly-ordered. <code>volatile</code> is an old, bad way to do this; C++11 std::atomic is a much better way to write lock-free code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload