title: Two threads accessing same variables, but one thread rarely happens. Non-atomics?
body: <p>I have two threads, t1 and t2, which access several variables (ints and doubles), lets call them a, b and c. t1 is on my critical path and increments/decrements these variables via an expensive compare-and-exchange. </p>
  
  <pre><code>std::atomic&lt;double&gt; a;
  std::atomic&lt;double&gt; b;
  std::atomic&lt;double&gt; c;
  std::atomic&lt;uint32_t&gt; d;
  .
  .
  .
  // Extremely-common, critical path thread t1
  d++;
  while(!a.compare_and_exchange(expectedVal, newVal);
  while(!b.compare_and_exchange(expectedVal, newVal);
  while(!c.compare_and_exchange(expectedVal, newVal);
  </code></pre>
  
  <p>t2 rarely occurs but when it does, it adds the variables mentioned above together.</p>
  
  <pre><code>// Rare thread, t2
  return a + b - c;
  </code></pre>
  
  <p>At the moment I use atomics. <strong>Is there any way I can declare the variables as non-atomic, because 99.999% of the time they are incremented from the same thread and on the "rare thread" I could use a memory barrier to ensure I cannot return <code>a + b - c</code> until the "critical path" thread has finished writing any stores?</strong></p>
  
  <p>This would allow me to add the latency only on the occasional situation the rare thread executes.</p>