gpt4 book ai didi

c++ - OMP threadprivate 对象未被破坏

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:01:24 27 4
gpt4 key购买 nike

底线

如何确保 threadprivate 实例被正确销毁?

背景

回答this question时在 VS2013 中使用英特尔 C++ 15.0 编译器时,我遇到了一个奇怪的问题。当声明一个全局变量 threadprivate 时,从线程拷贝不会被破坏。我开始寻找强制销毁它们的方法。在 this站点,他们说添加 OMP 屏障应该会有所帮助。它没有(见 MCVE)。我尝试将 OMP 阻塞时间设置为 0,这样线程就不会停留在并行区域之后(也没有帮助)。我尝试添加一些延迟主线程的虚拟计算,让其他线程有时间死掉。仍然没有帮助。

MCVE:

#include <iostream>
#include <omp.h>

class myclass {
int _n;
public:
myclass(int n) : _n(n) { std::cout << "int c'tor\n"; }

myclass() : _n(0) { std::cout << "def c'tor\n"; }

myclass(const myclass & other) : _n(other._n)
{ std::cout << "copy c'tor\n"; }

~myclass() { std::cout << "bye bye\n"; }

void print() { std::cout << _n << "\n"; }

void add(int t) { _n += t; }
};

myclass globalClass;
#pragma omp threadprivate (globalClass)

int main(int argc, char* argv[])
{
std::cout << "\nBegninning main()\n";

// Kill the threads immediately
kmp_set_blocktime(0);

#pragma omp parallel
{
globalClass.add(omp_get_thread_num());
globalClass.print();
#pragma omp barrier
//Barrier doesn't help
}

// Try some busy work, takes a few seconds
double dummy = 0.0;
for (int i = 0; i < 199999999; i++)
{
dummy += (sin(i + 0.1));
}
std::cout << dummy << "\n";

std::cout << "Exiting main()\n";
return 0;
}

输出是

def c'tor

Begninning main()
def c'tor
1
def c'tor
3
def c'tor
2
0
1.78691
Exiting main()
bye bye

只有一个“再见”,而我本以为会有四个。

更新

正在关注 Kyle's OMP 4.0 标准的引用

The storage of all copies of a threadprivate variable is freed according to how static variables are handled in the base language, but at an unspecified point in the program.

我添加了该类的一个静态实例(全局实例和本地实例)以查看其析构函数是否被调用。它确实适用于本地和全局案例。所以问题仍然存在。

最佳答案

这是有记录的行为(虽然我不知道为什么做出这个决定)。

来自MSDN entry on threadprivate (有一些格式更改):

A threadprivate variable of a destructable type is not guaranteed to have its destructor called.

...

Users have no control as to when the threads constituting the parallel region will terminate. If those threads exist when the process exits, the threads will not be notified about the process exit, and the destructor will not be called for threaded_var on any thread except the one that exits (here, the primary thread). So code should not count on proper destruction of threadprivate variables.

OpenMP version 4.0 standard未指定析构函数调用行为的顺序。来自 12.14.2 部分:

Page 151, lines 7-9:

The storage of all copies of a threadprivate variable is freed according to how static variables are handled in the base language, but at an unspecified point in the program.

Page 152, lines 8-10:

The order in which any constructors for different threadprivate variables of class type are called is unspecified. The order in which any destructors for different threadprivate C++ variables of class type are called is unspecified.

就我个人而言,在我看来,Microsoft 可能将此视为过多的空头支票;未指定析构函数顺序 似乎与未能完全 保证将调用析构函数有很大不同。在基本语言(本例中为 C++)中处理静态变量的方式是析构函数保证被调用。所以我认为 MSVC 不符合标准(C++ 标准和 OMP 标准),但由于我不是语言律师,所以不要相信我的话。

话虽如此,但很难看出这会产生怎样的严重影响。你当然不应该看到任何内存泄漏,因为 threadprivate 存储空间应该在创建/销毁线程时立即分配/释放。 (如果您的 threadprivate 实例引用了它们管理的非 threadprivate 内存,那么......这似乎首先不会起作用。)

关于c++ - OMP threadprivate 对象未被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32374778/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com