title: Atomic operation propagation/visibility (atomic load vs atomic RMW load)
body: <p><strong>Context</strong>&emsp;</p>
  
  <p>I am writing a thread-safe <a href="https://github.com/sm2coin/libcr" rel="nofollow noreferrer">protothread/coroutine library</a> in C++, and I am using atomics to make task switching lock-free. I want it to be as performant as possible. I have a general understanding of atomics and lock-free programming, but I do not have enough expertise to optimise my code. I did a lot of researching, but it was hard to find answers to my specific problem: <em>What is the propagation delay/visiblity for different atomic operations under different memory orders?</em></p>
  
  <p><strong>Current assumptions</strong>&emsp;</p>
  
  <p>I read that changes to memory are propagated from other threads, in such a way that they might become visible:</p>
  
  <ol>
  <li>in different orders to different observers,</li>
  <li>with <em>some</em> delay.</li>
  </ol>
  
  <p>I am unsure as to whether this delayed visibility and inconsistent propagation applies only to non-atomic reads, or to atomic reads as well, potentially depending on what memory order is used. As I am developing on an x86 machine, I have no way of testing the behaviour on weakly ordered systems.</p>
  
  <p><strong>Do all atomic reads always read the latest values, regardless of the type of operation and the memory order used?</strong>&emsp;</p>
  
  <p>I am pretty sure that all <em>read-modify-write</em> (RMW) operations <em>always</em> read the most recent value written by any thread, regardless of the memory order used. The same seems to be true for <em>sequentially consistent</em> operations, but only if all other modifications to a variable are also <em>sequentially consistent</em>. Both are said to be slow, which is not good for my task. If not all atomic reads get the most recent value, then I will have to use RMW operations just for reading an atomic variable's latest value, or use atomic reads in a while loop, to my current understanding.</p>
  
  <p><strong>Does the propagation of writes (ignoring side effects) depend on the memory order and the atomic operation used?</strong>&emsp;</p>
  
  <p><em>(This question only matters if the answer to the previous question is that not all atomic reads always read the most recent value. Please read carefully, I do not ask about the visibility and propagation of side-effects here. I am merely concerned with the value of the atomic variable itself.)</em> This would imply that depending on what operation is used to modify an atomic variable, it would be guaranteed that any following atomic read receives the most recent value of the variable. So I would have to choose between an operation guaranteed to always read the latest value, or use relaxed atomic reads, in tandem with this special write operation that guarantees instant visibility of the modification to other atomic operations.</p>