我正在通过 OpenMP 编写面向对象的多线程 C++ 代码。为了简单和通用,我不得不在模板类中定义一些变量和函数。我想让它们 threadprivate
以利用线程私有(private)变量的持久性特性,并能够在嵌入或调用模板类的其他函数或源文件中使用它们。但我收到以下错误:
“...不是文件、命名空间或 block 作用域变量”
我知道我只能对文件、命名空间或 block 作用域变量使用 threadprivate
指令,但我想知道是否还有其他方法可以为类成员应用 threadprivate
指令。我的总体代码布局如下:
template<typename T> class EntContainer
{
private:
T** empties;
e_NumPointers;
e_nLists;
.
.
.
public:
#pragma omp threadprivate(empties, e_NumPointers, e_nLists)
void function
{
// do some paralle stuff using #pragma omp parallel
}
}
提前致谢
最佳答案
OpenMP 3.0 draft allows threadprivate on static class members, but #pragma omp threadprivate must be present inside of the definition of the class containing the threadprivate static class member, and must lexically preceed all uses. source
将成员设为静态并将 #pragma omp threadprivate 指令移到它们的声明下方应该可以修复错误。
关于c++ - 如何为类成员变量定义threadprivate变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17179382/