- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有以下情况:在标题“test.hpp”中我定义:
class ObjectA {
public:
ObjectA();
~ObjectA();
static ObjectA & get_A();
};
class ObjectB {
public:
~ObjectB();
static ObjectB & get_B();
void do_cleanup();
};
在单独的编译单元中我实现了 ObjectB:
#include "test.hpp"
#include <iostream>
ObjectB::~ObjectB() {
std::cout<<"ObjectB dtor"<<std::endl;
}
ObjectB & ObjectB::get_B() {
thread_local ObjectB b_instance;
return b_instance;
}
void ObjectB::do_cleanup() {
std::cout<<"Clearing up B garbage..."<<std::endl;
}
对象A:
#include "test.hpp"
#include <iostream>
ObjectA::ObjectA() {
ObjectB::get_B(); <--dummy call to initialize thread_local ObjectB;
}
ObjectA::~ObjectA() {
std::cout<<"ObjectA dtor"<<std::endl;
ObjectB::get_B().do_cleanup(); // <-- is this undefined behaviour??
}
ObjectA & ObjectA::get_A() {
thread_local ObjectA a_instance;
return a_instance;
}
最后是测试 main():
#include <thread>
#include "test.hpp"
int main() {
std::thread check([](){
ObjectA::get_A(); //<--dummy call just to initialize thread_local object.
});
check.join();
return 0;
}
上面的程序是否表现良好或正在访问 objectB,它具有来自 ObjectA 析构函数的 thread_local 存储,而 ObjectA 析构函数也具有 thread_local 存储未定义行为?如果是这样,为什么它会损坏,我该如何修复它?
[编辑,@Soonts 回答]
在实际用例中,A 类是模板,一个相当复杂的模板,而 B 类只是很大。 A 对象使用 shared_ptr<> 保存对 B 的引用,并且 B 的 thread_locals 根据需要进行访问。 (A 在主线程中构造并传递给工作线程)因此,在调用 ObjectA::get_A() 之前,工作线程可能不会调用 ObjectB::get_B()。
最佳答案
规范说明了一些关于生命周期的事情:
线程存储时长。对象的存储在线程开始时分配,在线程结束时释放。每个线程都有自己的对象实例。
如果具有线程存储持续时间的对象的构造函数或动态初始化的完成排在另一个之前,则第二个析构函数的完成排在析构函数的启动之前第一个。
现在回到您的代码。
你构造A,在构造函数中构造B。因此,B构造函数的完成发生在A构造函数完成之前。根据上面的内容,当线程即将退出时,它会先销毁 A,然后销毁 B。根据规范的字母,您的代码是可以的。
实际上,我不确定 C++ 编译器是否将规范实现到那种程度的细节。如果我正在编写该代码,我就不会以这种方式使用 thread_local 对象。相反,我会将 B 放在 A 的非静态字段中。这比依赖语言标准的这种细微差别更简单,IMO 更可靠。
关于C++11 thread_local 析构函数行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51954025/
我是一名优秀的程序员,十分优秀!