V1205. Data race risk. Unprotected concurrent operation with the 'foo' variable

The analyzer has detected an error caused by concurrent operation with a shared variable. Let us consider a sample incorrect code:

int a = 0;
#pragma omp parallel for num_threads(4)
for (int i = 0; i < 100000; i++)
{
      a++;
}

Since all the threads write to the same memory space and read from the same memory space at the same time, the variable's value after this loop is unpredictable. In order to make the operation safe, one should either place the operation in a crtical section or, since the operation in the example above is atomic, use the "#pragma omp atomic" directive. The last option is more preferrable since it makes the resulting code faster:

int a = 0;
#pragma omp parallel for num_threads(4)
for (int i = 0; i < 100000; i++)
{
      #pragma omp atomic
      a++;
}

If the operation's type does not allow using the "#pragma omp atomic" directive, one can use critical sections or locks. Critical sections are more preferrable since they work faster:

int a = 0;
#pragma omp parallel for num_threads(4)
for (int i = 0; i < 100000; i++)
{
      #pragma omp critical
      {
            ...
      }
}

Please note that V1205 will not occur if the variable in question is local or if the code is executed only by a single thread (for example, if the operation is performed in a "#pragma omp single" block).