You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
More interestingly, the output can also be 0 20, even though there is no possible globally consistent order of the four operations that would result in this outcome. When 3 is executed, there is no happens-before relationship with 2, which means it could load either 0 or 20. When 4 is executed, there is no happens-before relationship with 1, which means it could load either 0 or 10. Given this, the output 0 20 is a valid outcome.
The question
Firstly, I want to thank you for writing this book and making it freely available online. I have been enjoying reading it. Recently, I was reading your book with a group, and almost all of us found this section somewhat difficult to understand.
Anyways to the question: How could 0 20 be possible if there are no possible globally consistent order of the four operations that would result in this outcome? I could only see it possible if instruction scheduling/reordering were allowed, which would make sense in this case since we have relaxed memory ordering. I thought with relaxed memory ordering the compiler should be able to reorder those instructions as relaxed memory ordering implies no additional dependency between the atomic memory related instructions.
However given the definition of the happens before relationship and figure 3.1, i.e. no instruction reordering within a thread:
The basic happens-before rule is that everything that happens within the same thread happens in order. If a thread is executing f(); g();, then f() happens-before g().
it causes me confusion.
The only legal states following the happens before relationship I thought would be:
1, 2, 3, 4: 10 20
1, 3, 2, 4: 10 0
1, 3, 4, 2: 10 0
3, 1, 2, 4 : 10 0
3, 1, 4, 2 : 10 0
3, 4, 1, 2 0 0
Also when running your provided code I get 10 20 ~ 92% of the times and the rest 0 0. Would it be possible somehow to force it to get 0 20 in this example? Perhaps a compiler flag?
Later on it does seem to be implied that this might only be the case when instruction scheduling/reordering is allowed:
Our intuitive understanding of the concept of "before" breaks down when things don’t necessarily happen in a globally consistent order, such as when instruction reordering is involved.
I am probably missing something, otherwise in my opinion it is inconsistent.
The text was updated successfully, but these errors were encountered:
The content that the question is about
https://marabos.nl/atomics/memory-ordering.html#happens-before
The question
Firstly, I want to thank you for writing this book and making it freely available online. I have been enjoying reading it. Recently, I was reading your book with a group, and almost all of us found this section somewhat difficult to understand.
Anyways to the question: How could
0 20
be possible if there are no possible globally consistent order of the four operations that would result in this outcome? I could only see it possible if instruction scheduling/reordering were allowed, which would make sense in this case since we have relaxed memory ordering. I thought with relaxed memory ordering the compiler should be able to reorder those instructions as relaxed memory ordering implies no additional dependency between the atomic memory related instructions.However given the definition of the happens before relationship and figure 3.1, i.e. no instruction reordering within a thread:
The only legal states following the happens before relationship I thought would be:
10 20
10 0
10 0
10 0
10 0
0 0
Also when running your provided code I get
10 20
~ 92% of the times and the rest0 0
. Would it be possible somehow to force it to get0 20
in this example? Perhaps a compiler flag?Later on it does seem to be implied that this might only be the case when instruction scheduling/reordering is allowed:
I am probably missing something, otherwise in my opinion it is inconsistent.
The text was updated successfully, but these errors were encountered: