title: InterlockedExchange on two CPU cores
body: <p>I have a Windows 7 driver where I want to synchronize access to a variable. Can I use InterlockedExchange for it?</p>
  
  <p>My current understanding of InterlockedExchange is, that InterlockedExchange is done via compiler intrinsics. That means, the read (InterlockedExchange returns the old value) and the write is done in one clock cycle. The interlocked functions are atomic only when the variable is always accessed via an interlocked function.</p>
  
  <p>But what happens in this case:</p>
  
  <pre><code>CPU1: InterlockedExchange(&amp;Adapter-&gt;StatusVariable, 5);
  CPU2: InterlockedExchange(&amp;Adapter-&gt;StatusVariable, 3);
  </code></pre>
  
  <p>StatusVariable is written in the same clock cycle on two CPU cores. Does the function notice that the variable is accessed and defer the write to a different clock cycle? Or is it undefined which value the variable has after the write? Is it also possible that the variable contains garbage?</p>
  
  <p>Edit: I am on x86 or x64.</p>