Quantcast
Channel: Atomic operation propagation/visibility (atomic load vs atomic RMW load) - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Atomic operation propagation/visibility (atomic load vs atomic RMW load)

$
0
0

Context

I am writing a thread-safe protothread/coroutine library 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: What is the propagation delay/visiblity for different atomic operations under different memory orders?

Current assumptions

I read that changes to memory are propagated from other threads, in such a way that they might become visible:

  1. in different orders to different observers,
  2. with some delay.

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.

Do all atomic reads always read the latest values, regardless of the type of operation and the memory order used?

I am pretty sure that all read-modify-write (RMW) operations always read the most recent value written by any thread, regardless of the memory order used. The same seems to be true for sequentially consistent operations, but only if all other modifications to a variable are also sequentially consistent. 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.

Does the propagation of writes (ignoring side effects) depend on the memory order and the atomic operation used?

(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.) 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.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images