title: Is this execution reordering possible
body: <p>As we know, the compiler or the CPU may reorder the execution as they want, only if they follow the as-if rule. For example, if we have such a piece of code:</p>
  <pre><code>C = A + B;
  D = E + F;
  </code></pre>
  <p>The compiler or the CPU may execute <code>D = E + F</code> before <code>C = A + B</code>. I can understand that.</p>
  <p>Today, my colleague tried to build a log library with C++. His idea is to use constructor &amp; destructor to make some logs with the help of some overloading stream functions, such as <code>operator&lt;&lt;()</code>.</p>
  <p>Basically, he offered such a kind of class:</p>
  <pre><code>class Log
  {
  public:
      Log() { streamObj &lt;&lt; &quot;start: &quot; &lt;&lt; getCurrentTime() &lt;&lt; endl; }
      ~Log() { streamObj &lt;&lt; &quot;end: &quot; &lt;&lt; getCurrentTime() &lt;&lt; endl; }
  };
  </code></pre>
  <p>Now, I'm the user of the log library. My colleague told me that I could use the library as below:</p>
  <pre><code>void func()
  {
      Log log;
      // do something1
      // do something2
      // do something3
      return;
  }
  </code></pre>
  <p>So when the first line is executed, the constructor is invoked so I can get a &quot;start&quot; in the log. And when the function returns, the destructor of <code>log</code> will be invoked so I will get an <code>end</code> in the log. With the help of the object <code>log</code>, we can clearly find the start of the function and the end of the function.</p>
  <p>That sounds clear and great.</p>
  <p>However, as I mentioned at the beginning of the post, the machine may do some reordering as it wants. So I'm now wondering if it is possible that the constructor of <code>log</code> is invoked later than we think and/or the destructor of the <code>log</code> is invoked sooner than we think so that the <code>log</code> can't work as we expected. I mean, the code of <code>func</code> did look as above but when it was compiled or was executed, the real order became:</p>
  <pre><code>// do something1
  Log log;
  // do something2
  // call the destructor of `log`
  // do something3
  return
  </code></pre>
  <p>BTW, the stream in the class <code>Log</code> is directed into somewhere else, such as a file, or a shared memory or a TCP socket.</p>
  <p>So am I reasonable? Or this kind of reordering will never happen? If it may happen, is there some technique to forbid this kind of reordering or some technique to offer an useable log library which can tell us the start and the end of any function?</p>
  <p>In fact, I've heard some new technique in C++11, such as <code>std::atomic</code> and <code>std::atomic_thread_fence</code>. As my understanding, if this kind of reordering is possible, what I need might be... a fence?</p>
  <pre><code>class Log
  {
  public:
      Log() {
          streamObj &lt;&lt; &quot;start: &quot; &lt;&lt; getCurrentTime() &lt;&lt; endl;
          // build a fence here!
      }
      ~Log() { streamObj &lt;&lt; &quot;end: &quot; &lt;&lt; getCurrentTime() &lt;&lt; endl; }
  };
  </code></pre>
  <p>I really don't know if it is possible...
  <br><br>
  <strong>About side effects/observable behavior</strong></p>
  <p>As my understanding, this is a side effect/observable behavior:</p>
  <pre><code>A = B + C
  </code></pre>
  <p>Why? Because the value of <code>A</code> is changed.</p>
  <p>Now, what if I code like this:</p>
  <pre><code>void func()
  {
      Log log;
      A = B + C;
  }
  </code></pre>
  <p>So the order could be:</p>
  <ul>
  <li>constructor of <code>log</code></li>
  <li><code>A = B + C</code></li>
  <li>destructor of <code>log</code></li>
  </ul>
  <p>However, if the order becomes:</p>
  <ul>
  <li><code>A = B + C</code></li>
  <li>constructor of <code>log</code></li>
  <li>destructor of <code>log</code></li>
  </ul>
  <p>I think that would be fine. Why? Because the value of <code>A</code>, which is about a side effect/observable behavior, is NOT modified. No matter which order we take, its value would be always <code>B + C</code>.</p>
  <p>Am I right? If I'm right, I think it means that the <code>Log</code> won't work as expected.</p>
  <p><strong>UPDATE</strong></p>
  <p><code>A = B + C</code> has a side effect, which is that the value of <code>A</code> is changed, but it isn't an observable behavior.</p>