简体   繁体   English

在多线程C ++程序中使用std :: vector时崩溃的应用程序

[英]crashing app when using std::vector in multithreaded C++ program

Simplified: I am writing plugins to realbasic. 简化:我正在为realbasic编写插件。 I have two plugins with their own threads. 我有两个具有各自线程的插件。 Everything is fine until i start to use std::vector. 一切都很好,直到我开始使用std :: vector为止。 I have vector<> variables in both plugins and I am not using the vectors to use same data or to share data between threads or anything else. 我在两个插件中都有vector <>变量,并且没有使用矢量来使用相同的数据或在线程或其他任何对象之间共享数据。 My model looks like this: 我的模型如下所示:

thread_1{
    vector<> variable_1;
    foreach{
         variable_1.push_back(something);
    }
}


thread_2{
    vector<> variable_2;
    foreach{
         variable_2.push_back(something);
    }
}

If I don't declare the vectors static, the program most of the times crashes. 如果我不将向量声明为静态,则大多数情况下程序会崩溃。 But even when i declare the variables static, and use some of algorithms like sort or copy, the crashes appear again. 但是,即使我声明变量为静态,并使用诸如排序或复制之类的某些算法,崩溃也会再次出现。 It seems like the operations on vector are using some abstract class which is not multithread safe or something. 似乎对vector的操作正在使用某些不是多线程安全的抽象类。 Or am I doing something wrong? 还是我做错了什么? Thank you. 谢谢。

I am using windows 7 x64, visual studio 2008 pro, compilation on release win32. 我正在使用Windows 7 x64,Visual Studio 2008 Pro,发行版win32上的编译。

AFAIK, the allocator is not thread safe. AFAIK,分配器不是线程安全的。 You should either allocate vector (reserve) in a thread safe way or use a thread safe allocator. 您应该以线程安全的方式分配向量(保留)或使用线程安全的分配器。

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

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