Question

Is there a need to be concerned about MemoryBarriers when using the Parallel Extensions?

Edit - to elaborate as the original question was open ended: (@xanatos' answer was the one I was looking for)

To give a concrete example: assume I use a Parallel.ForEach and each iteration sets the value of a property within the class (each iteration sets it's own specific property, no two iterations set the value of the same property). On the same thread that called the Parallel.ForEach, I access the properties that were set from the Parallel.ForEach.

class Program
{
  static void Main(string[] args)
  {
    var t = new Test();
    t.InitializePropertiesInParallel();
    var a = t.PropA; // Could never be 0?
    var b = t.PropB; // Could never be 0?
  }
}

public class Test
{
  public int PropA { get; set; }
  public int PropB { get; set; }

  public void InitializePropertiesInParallel()
  {
    var initializers = new List<Action<int>>()
    {
      i => PropA = i,
      i => PropB = i
    };

    initializers.AsParallel().ForAll(a => a(1));
  }
}
Was it helpful?

Solution

If every worker doesn't need data prepared by the other workers (so there isn't any situation where Worker 1 writes A and Worker 2 reads A) there isn't any need for MemoryBarrier. When all the tasks finish there is a Wait that acts as a MemoryBarrier (in the end, even if you don't see it, somewhere there is a synchronization construct that waits for all the workers to finish)

OTHER TIPS

Keep one thing in mind here: cache line invalidation. It's a complicated topic, but there's a great MSDN article on it here.

The gist of it in this particular example is that, because you're modifying the same object instance from two different threads, even though you not touching the exact same location in memory, you are going to be touching the same cache line and so when the first thread on processor 1 changes the memory, the cache will be flushed and updated before processor 2 can read/write to it again.

That said, again considering your specific example with just two props, this is a micro-optimization and absolutely not something to worry about. Just something to be aware of if you were to extrapolate upon it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top