1

I'm going through Mara's Atomics and locks book, which shows this example code where it assumes that the function a and b are executed concurrently:

static X: AtomicI32 = AtomicI32::new(0);

fn a() {
    X.fetch_add(5, Relaxed);
    X.fetch_add(10, Relaxed);
}

fn b() {
    let a = X.load(Relaxed);
    let b = X.load(Relaxed);
    let c = X.load(Relaxed);
    let d = X.load(Relaxed);
    println!("{a} {b} {c} {d}");
}

Ref: https://marabos.nl/atomics/memory-ordering.html#relaxed

It says that since only one thread modifies X, only one possible order of modification of X is possible: 0 -> 5 -> 15

My question is why it cannot be this instead: 0 -> 10 -> 15 because of the possiblity of reordering of instruction ? I ask this because earlier in this chapter this was specified:

The logic for verifying that a specific reordering or other optimization won’t affect the behavior of your program does not take other threads into account.

Because of that, I belive the compiler or processor is free to optimize and re-reorder if required.

4
  • Reordering statements where those statements effect can be observed is not allowed for either the CPU or the compiler. Commented Aug 23, 2024 at 15:42
  • 4
    @cafce25: That's not true in general. Two relaxed-atomic stores to different locations can become visible to other threads in an order different from program-order. The key factor here is that both operations are on the same object, and the object is atomic. Commented Aug 24, 2024 at 2:30
  • @PeterCordes That's not what I said though, I said those statements can't be reoredered, that's different from their effect becoming visible on another thread. Though admittedly it's less relevant. Commented Aug 24, 2024 at 5:46
  • 1
    @cafce25: You said reordering isn't allowed in any case where the difference is observable. That's not true for non-atomic or non-seq_cst memory orders on different objects when the observer is a different thread. Or was your use of "those" intended to limit it to the special case in the question? If that's what you intended, your meaning doesn't come across clearly. Commented Aug 24, 2024 at 10:01

1 Answer 1

4

There's reordering and there's reordering :)

Atomic instructions on a single atomic values are always ordered, regardless of the memory ordering used.

The memory ordering only affects the ordering of memory operations on other values (atomic or not) relatively to the atomic operation.

Sign up to request clarification or add additional context in comments.

3 Comments

In the C++ formalism used in the standard, the modification-order of a variable is compatible with sequenced-before, for modifications that come from the same thread. I assume Rust works the same way, if their formalism is remotely similar to C++'s.
@PeterCordes Yeah, Rust works the same way as C++. Can you put the relevant C++ formalism link here ? I was not able to find that for the same thread.
@Sibi: write-write coherence (eel.is/c++draft/intro.races#15) links happens-before to the modification order of a single object. And eel.is/c++draft/intro.races#10 - one way for A to happen before B is for it to be sequenced-before. In C++ terms, "sequenced before" is "program order in the same thread".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.