- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Link Copied
0 Replies
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page