Could someone explain with an example where it's clearly visible that Rel-Acq synchronization doesn't guarantee global ordering? I want to understand the essence of it
Thanks a lot!
The key to this is that what you read via an Acquire or Relaxed load, or that was written with a Release or Relaxed store, doesn't tell you anything about other atomics; all you know is the value of the atomic you're reading.
In contrast, with SeqCst loads and stores, the fact of reading a given value from one SeqCst atomic tells you something about all SeqCst atomics
Taken from a previous post of mine about SeqCst:
For example, if you have two atomics, A and B, both initialized to 0, and thread 1 does Release stores of 1 then 2 to atomic A, while thread 2 does Release stores of 3 then 4 to atomic B, all threads will agree that atomic A took on values 0, 1 and then 2 in sequence (noting that they might skip some entries in this sequence - you might only ever see 2, but you'll never see 1 then 0 then 2), while atomic B took on values 0, 3 and 4 in sequence (again, noting the possibility of skipping values but not reordering). But, it's possible for one thread doing Acquire loads of A and B to see the combined set being:
| A | B |
|---|---|
| 0 | 0 |
| 1 | 0 |
| 2 | 0 |
| 2 | 3 |
| 2 | 4 |
while another thread doing Acquire loads sees:
| A | B |
|---|---|
| 0 | 0 |
| 0 | 3 |
| 1 | 3 |
| 1 | 4 |
| 2 | 4 |
Both of these are consistent with the orderings defined by thread 1 and thread 2's Release stores (if you go down a column, you never see the atomic go "backwards" in the sequence), but it's possible for two threads to disagree on what pairs of values can be observed; you could have one thread see (A, B) = (2, 0), while another thread can never see (A, B) = (2, 0), but can see (A, B) = (1, 4), which is impossible to observe on a thread that's already observed (A, B) = (2, 0).
SeqCst requires that there's only one sequence of SeqCst writes that all threads using SeqCst reads agree on (in addition to all SeqCst writes being Releases and all SeqCst reads being Acquires); if you changed from Release stores and Acquire loads to SeqCst for both, only one of those tables could possibly be valid, and thus you know that if you're seeing (A, B) = (1, 4), another thread that does a SeqCst load cannot observe (A, B) = (2, 0) or (A, B) = (2, 3) (since that would mean that the other thread is observing a different sequence of writes to you).
Hey, thanks a lot for a detailed response!
I think Release-Acquire synchronization does associate different atomic variables. for example:
// both X and Y start with 0 as their init value.
// thread 1:
Y.store(1, Ordering:;Relaxed);
X.store(1, Ordering::Release);
// thread 2:
if X.load(Ordering::Acquire) == 1 {
// this assert can never fail.
assert!(Y.load(Ordering::Relaxed) == 1);
}
Please correct me if I've misunderstood something. thanks again!
edit: I've made some changes in the code snippet.
fantastic explanation! I think I understand it now : )
That's not associating different atomics; that works because implicit in the acquire-release pair is that the two threads synchronize - all stores by thread 1 before X.store are going to be visible to thread 2 after X.load, but that's because the threads synchronize as a result of the acquire-release pair, not because there's a relationship between the two atomics.
You need a third thread and a bit of a change to thread 2 to observe the difference between SeqCst and other orderings:
// both X and Y start with 0 as their init value.
// thread 1:
Y.store(1, Ordering::Relaxed);
X.store(1, Ordering::Release);
// thread 2:
if X.load(Ordering::Acquire) == 1 {
// this assert can never fail.
assert!(Y.load(Ordering::Relaxed) == 1);
Y.store(2, Ordering::Relaxed);
}
// thread 3:
if Y.load(Ordering::Relaxed) == 2 {
// WRONG: this assert can never fail
assert!(X.load(Ordering::Acquire) == 1);
}
The comment is wrong, because the lack of synchronization between thread 3 and the other two threads means that it could observe thread 2's write to Y before it observes thread 1's write to X - and that applies even though thread 2 writes to Y after observing thread 1's write to X.
If the accesses were all SeqCst, the comment wouldn't be wrong - there's a global ordering of all SeqCst writes, and thus the fact that thread 3 observes that thread 2 has stored to Y implies that thread 3 can also observe thread 1's store to X.
got it, thank you!
I also feel like it's easier to explain why you shouldn't use SeqCst if we massively balloon the thread count:
Thread 6 does a Release store that matches up with Thread 7's Acquire load.
Thread 8 does a Release store that matches up with Thread 9's Acquire load.
Thread 7 sees none of the non-atomic writes that Thread 8 was performing, and Thread 9 sees none of the non-atomic writes that Thread 6 was performing.
If these were SeqCst, then Thread 1, 2, 3, 4, 5, 7 and 9 would have to be able to agree on whether Thread 6 or 8's store happened first, and that's just a waste of resources most of the time.
Yeah, that makes a lot of sense : )
One has to think hard and annotate according to our requirements.
As a rule of thumb, you should only use SeqCst if you can point to a peer-reviewed paper that explains (a) why this algorithm only works with SeqCst and (b) why it's faster to use SeqCst than any other synchronization option for machines of the sort of scale you expect to run on (bearing in mind that SeqCst's guarantees get more expensive as the machine gets larger).
I've broken this rule when refactoring legacy single-threaded code to be multi-threaded (where it was understood that what I was doing was long-term wrong, but let us get a short-term gain until we had the time to refactor properly - and I left that job before the proper refactor had happened), but that's like any rule of thumb - you can knowingly break it, but you'd better understand why you're breaking it.
Indeed, the normal rules of thumb are:
- If you just want this atomic's changes to be visible across threads, use
Relaxed. - If you want to synchronize not just the atomic, but all past work, use
Releasestores andAcquireloads (orAcqRelread-modify-writes), and make sure you understand how they form a "synchronizes-with" point between a pair of threads. - Don't use
SeqCst.