C++ 哪个线程调用析构函数

时间:2021-03-24 12:45:57

标签: c++ multithreading thread-safety

如果我创建一个 <property name="eclipselink.id-validation" value="NULL"/> 对象,它的析构函数会在创建它的线程上调用:

thread_local

#include <iostream>
#include <thread>
#include <chrono>

struct MyStruct {
    ~MyStruct() {
        std::cout << "Destructed on thread #" << std::this_thread::get_id() << std::endl;
    }
};

void f() {
    thread_local MyStruct myStruct;
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

int main() {
    std::thread t(f);
    std::cout << "Created thread #" << t.get_id() << std::endl;
    t.join();
}

C++ 标准是否保证这种行为?它只说明了这一点:

<块引用>

一个有线程存贮期的变量必须先初始化 它的第一次 odr 使用 (6.2),如果建造,应在 线程退出。

0 个答案:

没有答案
相关问题