Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

openmp implementation: threadprivate behaviour

imakris
Beginner
184 Views

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?

0 Kudos
0 Replies
Reply