openmp implementation: threadprivate behaviour

openmp implementation: threadprivate behaviour

I noticed that although constructors ofglobal threadprivate objectsarecalled for every thread,constructors of local static threadprivate objects are not.

Example:

class A
{
public:
int v;
A() {
v = omp_get_thread_num();
printf("constructor called from thread %d ", omp_get_thread_num());
}
};

//first case: global object (runs as expected)

static A a;
#pragma omp threadprivate(a)

int main()
{
omp_set_num_threads(3);
#pragma omp parallel


printf("thread: %d, a.v: %d ", omp_get_thread_num(), a.v);

return 0;
}

the program output is:

constructor called from thread 0
thread: 0, a.v: 0
constructor called from thread 1
constructor called from thread 2
thread: 1, a.v: 1
thread: 2, a.v: 2

//second case: local object (constructors are not called for subsequent threads)

int main()
{
static A a;
#pragma omp threadprivate(a)

omp_set_num_threads(3);

#pragma omp parallel

printf("thread: %d, a.v: %d ", omp_get_thread_num(), a.v);

return 0;
}

this has the following output:

constructor called from thread 0
thread: 0, a.v: 0
thread: 2, a.v: 0
thread: 1, a.v: 0

Do you know why this might be happening? Is it possible that this is some problem with the compiler?

1 post / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.