任意一个并行域都不能嵌套在其他并行域中(Neither parallel region is nested inside another explicit parallel region.)
执行两个并行域的线程数量要相同(The number of threads used to execute both parallel regions is the same.)
执行两个并行域时的线程亲和度策略要相同( The thread affinity policies used to execute both parallel regions are the same.)
在进入并行域之前dyn-var变量的值必须为false(0). (The value of the dyn-var internal control variable in the enclosing task region is false at entry to both parallel regions.)
int counter = 10; #pragma omp threadprivate(counter)
voidtest_copyprivate(){ int i; #pragma omp parallel private(i) { #pragma omp single copyprivate(i, counter) { i = 50; counter = 100; printf("thread %d execute single\n", omp_get_thread_num()); } printf("thread %d: i is %d and counter is %d\n",omp_get_thread_num(), i, counter);
} }
下面是程序运行结果:
1 2 3 4 5
thread 3 execute single thread 2: i is 50 and counter is 100 thread 3: i is 50 and counter is 100 thread 0: i is 50 and counter is 100 thread 1: i is 50 and counter is 100
下面是将copyprivate(i, counter)去掉的运行结果
1 2 3 4 5
thread 0 execute single thread 2: i is 0 and counter is 10 thread 0: i is 50 and counter is 100 thread 3: i is 0 and counter is 10 thread 1: i is 32750 and counter is 10