为什么使用thread_local会崩溃

Why does this usage of thread_local crash?

本文关键字:local 崩溃 thread 为什么      更新时间:2023-10-16

我已经将问题简化为以下小代码片段:

struct tls {
    ~tls() {}
    void dont_opt_out() {}
};
thread_local tls tls_obj;
int main(int argc, char **argv) {
    tls_obj.dont_opt_out();
}

执行时程序崩溃。为什么?

我在Windows上使用的是gcc 4.8.2版本,我使用g++ -std=c++11 main.cpp进行编译。

尝试将代码编译为多线程。

在Cygwin下,它是编译器和链接器的-mthreads选项。