using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main()
{
double[] numbers = new double[10000];
Parallel.For(0, 9999, index => numbers[index] = index * 3);
Thread.MemoryBarrier();
// do something with numbers
}
}
Is the MemoryBarrier() required above, or does the Parallel.For have a ‘built in’ memory barrier?
There is no need for the memory barrier, when you do a Parallel.For the thread that calls the function waits for the end of the tasks, if it waits, it probably uses some synchronizer, to notify a waitting thread, when a synchronizer notifues “it is needed” to have a release barrier, a release barrier garanties that all changes done in the code above are visible globally, so you don’t need the memory.barrier