The Microsoft Parallel.For documentation contains the following method:
static void MultiplyMatricesParallel(double[,] matA, double[,] matB, double[,] result) { int matACols = matA.GetLength(1); int matBCols = matB.GetLength(1); int matARows = matA.GetLength(0);
In this method, potentially multiple threads read values ββfrom matA and matB that were created and initialized in the calling thread, and potentially multiple threads write values ββto result , which are later read by the calling thread. Within the lambda that passed before Parallel.For , an explicit lock around the array is not read or written. Since this example comes from Microsoft, I assume it is thread safe, but I'm trying to figure out what is going on behind the scenes to make it thread safe.
As far as I understand from the read and other questions that I asked on SO (for example this one ), several memory barriers are required for this to work. It:
- memory barrier in the calling thread after creating and initializing
matA and matB , - memory barrier for each non-calling thread before reading values ββfrom
matA and matB , - memory barrier for each non-calling thread after writing the values ββof
result and - memory barrier in the calling thread before reading values ββfrom
result .
Did I understand this correctly?
If so, does Parallel.For all this somehow? I went to delve into the original source, but had problems after the code . I have not seen any lock blocks or MemoryBarrier calls.
adv12 source share