V1212. Data race risk. When accessing the array 'foo' in a parallel loop, different indexes are used for writing and reading

13.06.2012

The analyzer detected a potential error in the code related to accessing array items in a parallel loop. The access is performed using different indexes and it may cause a race condition. Check if your code contains an error similar to this one:

#pragma omp parallel for
for (ptrdiff_t i = 1; i < len; i++)
{
  arr[i - 1] += arr[i];
}

In this code we add each array item to the following one. The code was parallelized incorrectly, so having the input sequence "1 1 1 1 1 1" we expect "2 2 2 2 2 1" but may get "2 2 3 2 2 1" instead.

To correct such errors you should change the algorithm and/or employ synchronization mechanisms.

Here is an example the analyzer considers correct because it does not contain calls to one array with different indexes:

#pragma omp parallel for num_threads(4)
for (int i = 0; i < n; i++)
{
  Array[i] = 0.0;
}