Understanding threadprivate support for C++

Hi all,

I am having some trouble understanding all the possible scenarios of the threadprivate construct with C++ object.

I have the following example(full example attached):

myclass x;

#pragma omp threadprivate (x)

#pragma omp parallel


int myid = omp_get_thread_num();



#pragma omp parallel


printf("%d\n", x.get());


In this example I understand that “x” will be threadprivate to each thread and its default constructor will be called when entering the first parallel region. In this case I am initializing an int (within the object) to 0 with the default constructor, then the method “add(myid)” will add the value of the thread id to this int.In the second parallel region each thread will basically print the value of it’s own thread id.

Another situation is when I put the “copyin(x)” clause in the first parallel construct. In that case the copy assignment operator will be called and the threadprivate version of x will be initialized to whatever value has the global instance of x.

If what I said is correct, what I don’t understand are the cases when the copy constructor and the “constructor accepting argument” would be called, what are possible examples? Also, what are examples with a vector (meaning the case that would call __kmpc_threadprivate_register_vec)?

Thank you.


threadprivate_defaultc.cpp (1.14 KB)