c++ - 使用std::multimap时c++发生运行时错误的机会很小

标签 c++ stl multimap

有时我在使用multimap std::async时遇到运行时错误。在 Debug模式下的Visual2019显示此错误:

Expression: cannot dereference end map/set iterator.



产生错误的代码示例:
#include <iostream>
#include <map>
#include <future>
#include <mutex>
#include <Windows.h>

class MyClass
{
public:
    MyClass()
    {

        mp.emplace(mapsize, 'f');
        mapsize += 1;
        ft = std::async([this]() {
            mx.lock();
            while (true) {
                for (int i = 0; i < mapsize; i++) {
                    auto pr = mp.equal_range(i);
                    for (auto j = pr.first; j != pr.second; j++)
                        std::cout << j->second << "\n";}}
            mx.unlock(); });
    }
private:
    std::mutex mx;
    static int mapsize;
    std::future <void>ft;
    static std::multimap <int, char> mp;
};
int MyClass::mapsize;
std::multimap <int, char> MyClass::mp;


int main()
{
    for (int i = 0; i < 100000; i++)
        new MyClass();
}

编辑:我做了一些同步,但它仍然会产生相同的错误

最佳答案

默认情况下,std::async在单独的线程中运行。因此,您将在不同步的情况下从多个线程访问同一对象(mp)。这被称为竞争条件,一种undefined behavior

仅在(1)多个读取器,0个写入器或(2)0个读取器,1个写入器的情况下,才可以对同一对象进行非同步并行访问。在所有其他情况下,访问应被序列化,例如,使用mutex

但是请注意,使用互斥锁时,对共享对象的所有访问都必须受同一互斥锁保护。因此,互斥锁应该被设置为static,并且也可以在mp.emplacemapsize += 1周围使用。

另外,为了更好的异常安全性,请使用unique_locklock_guard(RAII)代替手动锁定互斥锁:

class MyClass
{
public:
    MyClass()
    {
        std::lock_guard<std::mutex> lock(mtx);
        mp.emplace(mapsize, 'f');
        mapsize += 1;
        ft = std::async([this]() {
            while (true) {
                std::lock_guard<std::mutex> lock(mtx);
                for (int i = 0; i < mapsize; i++) {
                    auto pr = mp.equal_range(i);
                    for (auto j = pr.first; j != pr.second; j++)
                        std::cout << j->second << "\n";
                }
            }
        });
    }
private:
    std::future <void>ft;
    static std::mutex mtx; // protects everything from here on down
    static int mapsize;
    static std::multimap <int, char> mp;
};
int MyClass::mapsize;
std::mutex MyClass::mtx;
std::multimap <int, char> MyClass::mp;

int main()
{
    for (int i = 0; i < 100000; i++)
        new MyClass();
}

关于c++ - 使用std::multimap时c++发生运行时错误的机会很小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62425505/

相关文章:

用于存储思维导图等结构的数据库

c++ - 在编译器中禁用默认数字类型

c++ - 为什么我不能在 STL 集中插入 600 万个元素?

Java Guava : Remove and put back elements from Multimap while iterating

c++ - multimap 运算符函数错误

c++ - STL:在海量数据中进行排序和搜索

c++ - std::unordered_multimap 中元素的顺序

c++ - 为什么 google styleguide 建议对大整数默认使用可选的 int64_t?

python - 如何使用 SWIG 将 C++ 数组转换为 Python 列表?

c++ - boost 灵气 : binding to struct with vector of tuples