OpenMP threadprivate问题error: automatic variable ‘a1’ cannot be ‘threadprivate’
RT
int a1;
#pragma omp threadprivate(a1)
error: automatic variable ‘a1’ cannot be ‘threadprivate’
貌似加上static 就可以了。 threadprivate必须要全局变量或静态变量?
但是线程内的一份拷贝而已,又不能改变a1的值,那这和private还有啥区别呢?
labler
10 years, 5 months ago
Answers
threadprivate指定的私有变量是全生命周期的,而不是仅限于当前语句块。
比如下面这段程序:
#include <omp.h>
#include <stdio.h>
int a = 10, i;
int main(){
#pragma omp threadprivate(a)
{
#pragma omp parallel for
for(i = 0; i<4; i++){
i = omp_get_thread_num();
a = i+2;
}
}
printf("%d\n", a);
#pragma omp threadprivate(a)
{
#pragma omp parallel for
for(i = 0; i<4; i++){
printf("tid = %d, a = %d\n", omp_get_thread_num(), a);
}
}
return 0;
}
其输出是:
2
tid = 2, a = 4
tid = 0, a = 2
tid = 3, a = 5
tid = 1, a = 3
猫井喵太郎
answered 10 years, 5 months ago