Why do I need a memory barrier?

10,962

Solution 1

Barrier #2 guarentees that the write to _complete gets committed immediately. Otherwise it could remain in a queued state meaning that the read of _complete in B would not see the change caused by A even though B effectively used a volatile read.

Of course, this example does not quite do justice to the problem because A does nothing more after writing to _complete which means that the write will be comitted immediately anyway since the thread terminates early.

The answer to your question of whether the if could still evaluate to false is yes for exactly the reasons you stated. But, notice what the author says regarding this point.

Barriers 1 and 4 prevent this example from writing “0”. Barriers 2 and 3 provide a freshness guarantee: they ensure that if B ran after A, reading _complete would evaluate to true.

The emphasis on "if B ran after A" is mine. It certainly could be the case that the two threads interleave. But, the author was ignoring this scenario presumably to make his point regarding how Thread.MemoryBarrier works simpler.

By the way, I had a hard time contriving an example on my machine where barriers #1 and #2 would have altered the behavior of the program. This is because the memory model regarding writes was strong in my environment. Perhaps, if I had a multiprocessor machine, was using Mono, or had some other different setup I could have demonstrated it. Of course, it was easy to demonstrate that removing barriers #3 and #4 had an impact.

Solution 2

The example is unclear for two reasons:

  1. It is too simple to fully show what's happening with the fences.
  2. Albahari is including requirements for non-x86 architectures. See MSDN: "MemoryBarrier is required only on multiprocessor systems with weak memory ordering (for example, a system employing multiple Intel Itanium processors [which Microsoft no longer supports]).".

If you consider the following, it becomes clearer:

  1. A memory barrier (full barriers here - .Net doesn't provide a half barrier) prevents read / write instructions from jumping the fence (due to various optimisations). This guarantees us the code after the fence will execute after the code before the fence.
  2. "This serializing operation guarantees that every load and store instruction that precedes in program order the MFENCE instruction is globally visible before any load or store instruction that follows the MFENCE instruction is globally visible." See here.
  3. x86 CPUs have a strong memory model and guarantee writes appear consistent to all threads / cores (therefore barriers #2 & #3 are unneeded on x86). But, we are not guaranteed that reads and writes will remain in coded sequence, hence the need for barriers #1 and #4.
  4. Memory barriers are inefficient and needn't be used (see the same MSDN article). I personally use Interlocked and volatile (make sure you know how to use it correctly!!), which work efficiently and are easy to understand.

Ps. This article explains the inner workings of x86 nicely.

Share:
10,962
hackerhasid
Author by

hackerhasid

hacker, founder of bargeapp.com - load testing for developers

Updated on June 02, 2022

Comments

  • hackerhasid
    hackerhasid almost 2 years

    C# 4 in a Nutshell (highly recommended btw) uses the following code to demonstrate the concept of MemoryBarrier (assuming A and B were run on different threads):

    class Foo{
      int _answer;
      bool complete;
      void A(){
        _answer = 123;
        Thread.MemoryBarrier(); // Barrier 1
        _complete = true;
        Thread.MemoryBarrier(); // Barrier 2
      }
      void B(){
        Thread.MemoryBarrier(); // Barrier 3;
        if(_complete){
          Thread.MemoryBarrier(); // Barrier 4;
          Console.WriteLine(_answer);
        }
      }
    }
    

    they mention that Barriers 1 & 4 prevent this example from writing 0 and Barriers 2 & 3 provide a freshness guarantee: they ensure that if B ran after A, reading _complete would evaluate to true.

    I'm not really getting it. I think I understand why Barriers 1 & 4 are necessary: we don't want the write to _answer to be optimized and placed after the write to _complete (Barrier 1) and we need to make sure that _answer is not cached (Barrier 4). I also think I understand why Barrier 3 is necessary: if A ran until just after writing _complete = true, B would still need to refresh _complete to read the right value.

    I don't understand though why we need Barrier 2! Part of me says that it's because perhaps Thread 2 (running B) already ran until (but not including) if(_complete) and so we need to insure that _complete is refreshed.

    However, I don't see how this helps. Isn't it still possible that _complete will be set to true in A but yet the B method will see a cached (false) version of _complete? Ie, if Thread 2 ran method B until after the first MemoryBarrier and then Thread 1 ran method A until _complete = true but no further, and then Thread 1 resumed and tested if(_complete) -- could that if not result in false?

  • hackerhasid
    hackerhasid almost 14 years
    Thank you, that was helpful. I guess I wasn't as clueless as I thought.
  • Ohad Schneider
    Ohad Schneider almost 13 years
    I don't understand both barrier 2 and 3 are needed in the case that B runs after A. Both are full fences, so any one of them would do alone, would it not ?
  • Brian Gideon
    Brian Gideon almost 13 years
    @ohadsc: Memory barriers influence the behavior of a single thread only. Consider that A and B may be running on different CPUs. If you removed barrier 2 then the write might not be commited. If you removed barrier 3 then the read might not be refreshed. The barriers in A have no impact on the execution of B and vice versa.
  • Ohad Schneider
    Ohad Schneider almost 13 years
    Thanks, I understand now. If you get the time, please see my question regarding your answer here: stackoverflow.com/questions/6574389/…
  • Erti-Chris Eelmaa
    Erti-Chris Eelmaa about 11 years
    I don't understand memory barrier#4(is it necessary?). The #3 already makes sure that we "invalidate" memory cache and have up to date values. And _answer is guaranteed to have value first. What am I missing?
  • Brian Gideon
    Brian Gideon over 10 years
    @Erti-ChrisEelmaa: Barrier #4 prevents _answer from being read before _complete which could result in the program printing 0 if A and B are interleaving.