繁体   English   中英

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

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

因此,我需要为我的应用程序创建一个线程池,这导致我创建了一个std::map<int, std::thread>对象。 我遇到了一些非常意外的行为,可以将其简化为:

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();

输出是

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

之后,将调用std::terminate() ,程序将接收SIGABRT信号。

实时调试表明正在调用terminate ,因为joinable()为true,但是我只是检查了一下,发现不是这样!

而且,克服它的方法就是在join()之后添加以下行:

threads.erase(1);

这让我有些困惑,因为它看起来像是在我的insert调用之前创建的std::thread的新实例...有人可以提示我这种意外行为吗?

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

如果容器尚未包含具有等效键的元素,则将其插入容器。

您的地图已经在键1处包含一个元素,因此第二个threads.insert不执行任何操作。 您只是想在同一std::thread上加入两次。 这就是为什么threads.erase(1); 解决了问题,您的地图不再在键1处包含线程。

暂无
暂无

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

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