title: Handling out of order execution
body: <p>I recently stumbled upon this <a href="http://en.wikipedia.org/wiki/Memory_barrier" rel="noreferrer">Wikipedia article</a>. From my experience with multi-threading I am aware of the multitude of issues caused by the program being able to switch threads between threads at any time. However, I never knew that compiler and hardware optimisations could reorder operations in a way that is guaranteed to work for a single thread, but not necessarily for multi-threading. Can anyone explain how to correctly deal with the possibility of reordered operations in a multi-threaded environment?</p>
  
  <p><strong>UPDATE</strong>: I originally had accidentally linked to the <a href="http://en.wikipedia.org/wiki/Out-of-order_execution" rel="noreferrer">Out-of-Order Execution</a> article instead of the <a href="http://en.wikipedia.org/wiki/Memory_barrier" rel="noreferrer">Memory barrier</a> article, which has a better explanation of the problem.</p>