小编Ant*_*res的帖子

thread_local shared_ptr 对象在销毁时导致 sigsegv

我有一个程序,用于thread_local std::shared_ptr管理一些主要在本地线程访问的对象。但是,当线程加入并且线程局部shared_ptr正在析构时,如果程序是由MinGW(Windows 10)编译的,那么在调试时总是会出现SIGSEGV。以下是重现该错误的最少代码:

// main.cpp
#include <memory>
#include <thread>

void f() {
    thread_local std::shared_ptr<int> ptr = std::make_shared<int>(0);
}

int main() {
    std::thread th(f);
    th.join();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如何编译:

g++ main.cpp -o build\main.exe -std=c++17
Run Code Online (Sandbox Code Playgroud)

编译器版本:

>g++ --version
g++ (x86_64-posix-seh-rev2, Built by MinGW-W64 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

使用 gdb …

c++ multithreading mingw mingw-w64 c++11

11
推荐指数
1
解决办法
256
查看次数

标签 统计

c++ ×1

c++11 ×1

mingw ×1

mingw-w64 ×1

multithreading ×1