roosevelt -
Here is a stupid little program to try and show you what is going on.
- Code: Select all
#include <omp.h>
#include <stdio.h>
int main(void)
{
int a = 5;
int *p;
omp_set_dynamic(0);
omp_set_num_threads(2);
p = &a;
printf("loc(p): %u loc(*p): %u *p = %i\n", &p, &*p, *p);
#pragma omp parallel firstprivate(p)
{
printf("t#: %i loc(p): %u loc(*p): %u *p = %i\n", omp_get_thread_num(), &p, &*p, *p);
*p = 10;
}
return 0;
}
The output looks like:
- Code: Select all
loc(p): 4290772056 loc(*p): 4290772060 *p = 5
t#: 0 loc(p): 4290771020 loc(*p): 4290772060 *p = 5
t#: 1 loc(p): 4273995260 loc(*p): 4290772060 *p = 10
As you can see from this, the pointer p has been privatized (all of the loc(p) values are different). However, what p is pointing to - the variable a - has not (all of the loc(*p) are the same). If you run this program multiple times, you will see that the value of *p printed within the parallel region is sometimes 5 for both print lines and sometimes 5 for one line and 10 for the other. There is indeed a data race between lines 17 (the print of "a" or *p) and 18 (the setting of "a" or *p).