简体   繁体   English

使用std :: map和std :: thread的意外行为

[英]Unexpected behavior using std::map with std::thread

So I need a thread pool for my application, which led me to create a std::map<int, std::thread> object. 因此,我需要为我的应用程序创建一个线程池,这导致我创建了一个std::map<int, std::thread>对象。 I've encountered some very unexpected behavior, which can be simplified to this: 我遇到了一些非常意外的行为,可以将其简化为:

std::map<int, std::thread> threads;

threads.insert(std::pair<int, std::thread>(1, std::thread([]() {
        std::cout << "I'm the first thread and I'm gonna work\n";
    })));
threads[1].join();

std::cout << "Thread 1 joinable? " << threads[1].joinable() << "\n";

threads.insert(std::pair<int, std::thread>(1, std::thread([]() {
        std::cout << "I'm not gonna work at all\n";
    })));
threads[1].join();

The output is 输出是

I'm the first thread and I'm gonna work
Thread 1 joinable? 0

Right after, std::terminate() is called and program receives SIGABRT signal. 之后,将调用std::terminate() ,程序将接收SIGABRT信号。

Live debugging suggested that terminate is being called because joinable() is true, but I just checked and figured it's not! 实时调试表明正在调用terminate ,因为joinable()为true,但是我只是检查了一下,发现不是这样!

Moreover, the way to overcome it was simply to add the following line after join() ing: 而且,克服它的方法就是在join()之后添加以下行:

threads.erase(1);

That leaves me a bit confused, as it looks like a new instance of std::thread was created just before my insert call... Can someone hint me about this unexpected behavior? 这让我有些困惑,因为它看起来像是在我的insert调用之前创建的std::thread的新实例...有人可以提示我这种意外行为吗?

From http://en.cppreference.com/w/cpp/container/map/insert : http://en.cppreference.com/w/cpp/container/map/insert中

Inserts element(s) into the container, if the container doesn't already contain an element with an equivalent key. 如果容器尚未包含具有等效键的元素,则将其插入容器。

Your map already contains an element at key 1 so the second threads.insert doesn't do anything. 您的地图已经在键1处包含一个元素,因此第二个threads.insert不执行任何操作。 You are simply trying to join twice on the same std::thread . 您只是想在同一std::thread上加入两次。 This is why threads.erase(1); 这就是为什么threads.erase(1); solves the problem, your map no longer contains a thread at key 1 . 解决了问题,您的地图不再在键1处包含线程。

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

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