在另一个线程中创建新条目时,Std :: map会抛出bad_alloc

时间:2017-07-18 13:04:36

标签: c++ multithreading

我正在使用GCC 4.4.7。我有一张map<int, string>类型的地图。我发现每当我试图从另一个线程访问它时,就会抛出terminate called without an active exception。它在我初始化主线程中的一个条目然后在另一个线程中访问它时工作。我使用此代码重现了错误

#include <map>
#include <string>
#include <iostream>   
#include <thread>

using namespace std;



class A {
public:
    void start() {
        while(1) {
            mp["random"] = 2;
        }
    }
private:
    map< int , string > mp_1;
    map<string, int> mp;

};

int main() {
    A * a = new A();
    std::thread t;
    cout << "opening new thread" << std::endl;
    t = std::thread(std::bind(&A::start, a));
}

有人可以帮忙吗?感谢。

0 个答案:

没有答案