简体   繁体   English

C ++程序结束时的thread_local向量分段错误

[英]thread_local vector segmentation fault at end of program in C++

I'm trying to make a multi-threaded program but am having a weird problem with thread local vectors. 我正在尝试制作一个多线程程序但是线程局部向量有一个奇怪的问题。 Here's the (stripped-down to only have the error) code: 这是(精简到只有错误)代码:

#include <vector>
#include <iostream>
thread_local std::vector<int> vec;
int main(){
    vec.push_back(3);
    std::cout << vec[0];
    // Make you push enter to show the error is at the end
    std::cin.ignore();
}

The program compiles fine and runs mostly fine, but after I press enter I get a "program has stopped working" message. 该程序编译良好,运行大多数罚款,但在我按回车后,我得到一个“程序已停止工作”的消息。 I ran it in gdb and got this error: 我在gdb中运行它并得到此错误:

Program received signal SIGSEGV, Segmentation fault.
0x004030b0 in std::vector<int, std::allocator<int> >::~vector() ()

Somehow, there's an error in the vector's destructor. 不知何故,向量的析构函数中存在错误。 Going through it with a breakpoint shows that this error happens as the program is quitting. 使用断点查看它会显示该错误在程序退出时发生。

Making the vector not thread local makes the program work, but of course I need it to be thread local. 使向量不是本地的线程使程序工作,但当然我需要它是线程本地的。 If I don't interact with the vector at all, the program works fine. 如果我根本不与矢量交互,程序运行正常。

I'm thinking I'll have to use some alternative, but does anybody know a way to make this work? 我想我将不得不使用一些替代方案,但有人知道一种方法来使这项工作? Thanks! 谢谢!

EDIT: I'm stupid and forgot more info on my system. 编辑:我很愚蠢,忘了更多关于我的系统的信息。 I'm using Windows Vista 64-bit. 我正在使用Windows Vista 64位。 Running g++ -v gives me: 运行g ++ -v给了我:

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/MinGW/bin/../libexec/gcc/i686-w64-mingw32/5.2.0/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with [edited out because it's too big and probably not relevant]
Thread model: posix
gcc version 5.2.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project)

The thread model might actually have a lot to do with it. 线程模型实际上可能与它有很大关系。

It is setup dependent; 它取决于设置; I have successfully compiled and run your example program on Windows 7 (64 bit) with the 64 bit MinGW. 我已经使用64位MinGW在Windows 7(64位)上成功编译并运行了您的示例程序。 Perhaps it is related to the fact that you use the 32 bit MinGW installation on 64 bit platform? 也许这与您在64位平台上使用32位MinGW安装有关? My g++ -v gives the following (same version and thread model as yours, different arch): 我的g ++ -v给出了以下内容(与您的版本和线程模型相同,不同的arch):

Using built-in specs.
COLLECT_GCC=g++
Target: x86_64-w64-mingw32
Thread model: posix
gcc version 5.2.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM