1

When a thread, T, reads an object at any point in time, the value it sees could be:

  1. The initial value of the object.
  2. A value stored by T.
  3. A value stored by another thread.

And there are memory barriers (Load-Load, Load-Store, Store-Load, and Store-Store), which can be defined as:

<Loads|Stores> before the barrier can't be reordered past the barrier and <Loads|Stores> after the barrier can't get reordered prior to the barrier.

Now, assume that a thread T1 is executing this piece of code:

load(X)
store(Y, 1)
-----------  <-- a barrier
load(Y)
store(X, 1)

while another thread, T2, is executing this piece of code:

load(Y)
------- <--- a barrier
load(X)

What would be the effect of the barriers in T1 and T2 (assume they are the same) on the visibility of X and Y in thread T2, when the barriers are:

  1. Load-Load
  2. Load-Store
  3. Store-Load
  4. Store-Store

Thanks!

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
cmutex
  • 1,478
  • 11
  • 24
  • 1
    You don't choose a program based on the barrier. You choose a barrier based on the program. What is the programming problem you're having? This so far is a theoretical question about computer memory architecture. For example, "I wrote this program, but I'm concerned about memory barriers. What is the correct barrier to use here?" – Raymond Chen Jul 27 '20 at 16:58
  • Depending on the type of the object and the characteristics of the machine, your 3 possibilities in the first paragraph could be accompanied by "4. Something completely different." E.g., for a type that isn't atomic. – Nate Eldredge Jul 27 '20 at 17:16
  • Since T2 has no barriers, the barrier in T1 has no effect on T2. – R.. GitHub STOP HELPING ICE Jul 27 '20 at 17:18
  • @R..GitHubSTOPHELPINGICE thanks! I will modify the question a bit. – cmutex Jul 27 '20 at 17:21
  • My answer on your newer question ([memory\_order\_relaxed and visibility](https://stackoverflow.com/a/66128464)) may shed some light on this: barriers just order this core's access to coherent shared memory. They can't make other cores wait for this core's stores. – Peter Cordes Feb 09 '21 at 23:19

0 Answers0