title: How to understand RELAXED ORDERING in std::memory_order (C++)
body: <p>I have read a lot of posts and watched several Youtube videos C++ atomic and memory model (ConCpp 17, 14).</p>
  
  <p>When I read the book <em>Concurrency In Action</em>, section 5.3.3, <strong>RELAXED ORDERING</strong>, I still cannot understand the example provided by the author under his assumptions.</p>
  
  <h2>Assumptions by the author</h2>
  
  <blockquote>
    <p>It’s not just that the compiler can reorder the instructions. <strong>Even if the threads are running the same bit of code</strong>, they can disagree on the order of events because of operations in other threads in the absence of explicit ordering constraints, because the different CPU caches and internal buffers can hold different values for the same memory. It’s so important I’ll say it again: threads don’t have to agree on the order of events.
    Not only do you have to throw out mental models based on interleaving operations, <strong>you also have to throw out mental models based on the idea of the compiler or processor reordering the instructions.</strong></p>
  </blockquote>
  
  <p>Suppose that <strong>the code we see is not reordered</strong>.</p>
  
  <h2>The example code:</h2>
  
  <pre><code>#include &lt;atomic&gt;
  #include &lt;thread&gt;
  #include &lt;assert.h&gt;
  
  std::atomic&lt;bool&gt; x,y;
  std::atomic&lt;int&gt; z;
  
  void write_x_then_y()
  {
      x.store(true,std::memory_order_relaxed); // 1
      y.store(true,std::memory_order_relaxed); // 2
  }
  
  void read_y_then_x()
  {
      while(!y.load(std::memory_order_relaxed)); // 3
      if(x.load(std::memory_order_relaxed))      // 4
          ++z;
  }
  
  int main() {
      x=false;
      y=false;
      z=0;
  
      std::thread a(write_x_then_y);
      std::thread b(read_y_then_x);
      a.join();
      b.join();
  
      assert(z.load()!=0); // 5
  }
  </code></pre>
  
  <p>from this link: <a href="https://www.developerfusion.com/article/138018/memory-ordering-for-atomic-operations-in-c0x/" rel="noreferrer">https://www.developerfusion.com/article/138018/memory-ordering-for-atomic-operations-in-c0x/</a></p>
  
  <p><a href="https://i.stack.imgur.com/7F0Yw.png" rel="noreferrer"><img src="https://i.stack.imgur.com/7F0Yw.png" alt="enter image description here"></a></p>
  
  <p>Why <code>x.load(relaxed)</code> return <code>false</code> but <code>y.load(relaxed)</code> return <code>true</code>? </p>
  
  <h2>The conclusion by the author</h2>
  
  <blockquote>
    <p>This time the assert (5) can fire, because the load of x (4) can read false, even though the load of y (3) reads true and <strong>the store of x (1) happens-before the store of y (2).</strong> x and y are different variables, so there are no ordering guarantees relating to the visibility of values arising from operations on each.</p>
  </blockquote>
  
  <h2>Q. Why load of x can be false?</h2>
  
  <p>The author concludes that assert can fire. So, <code>z</code> can be <code>0</code>.
  So, <code>if(x.load(std::memory_order_relaxed))</code> : <code>x.load(std::memory_order_relaxed)</code> is <code>false</code>.</p>
  
  <p>But anyway, <code>while(!y.load(std::memory_order_relaxed));</code> makes <code>y</code> <code>true</code>.</p>
  
  <p>If we don't reorder the code sequence of (1) and (2), <strong>how is it possible that <code>y</code> is true but <code>x</code> is still not be stored?</strong></p>
  
  <p><strong>How to understand the figure provided by the author?</strong></p>
  
  <p>Based on <code>the store of x (1) happens-before the store of y (2)</code>, if <code>x.store(relaxed)</code> happen-before <code>y.store(relaxed)</code>, <code>x</code> should be <code>true</code> now. But why <code>x</code> is still <code>false</code> even <code>y</code> is <code>true</code>?</p>