为什么使用boost的Concurrent Hasmap不起作用

时间:2016-04-08 10:23:53

标签: c++ multithreading boost

我有以下并发哈希映射:

template<typename Key, typename Value>
class ConcurrentMap
{

public:
    ConcurrentMap() {};
    bool has(Key k)
    {
        boost::shared_lock<boost::shared_mutex> lock(schemaAccess);
        return m.find(k) != m.end();
    }

    void set(Key k, Value v)
    {
        boost::shared_lock<boost::shared_mutex> lock(schemaAccess);

        // set k, v
        if(m.find(k) != m.end())
        {
            boost::upgrade_lock<boost::shared_mutex> valueLock(*valueAccess[k]);
            boost::upgrade_to_unique_lock<boost::shared_mutex> valueUniqueLock(valueLock);

            m.at(k) = v;
        }
        // insert k, v
        else
        {
            lock.unlock();
            boost::upgrade_lock<boost::shared_mutex> schemaLock(schemaAccess);
            boost::upgrade_to_unique_lock<boost::shared_mutex> schemaUniqueLock(schemaLock);

            boost::shared_ptr<boost::shared_mutex> mtx = boost::make_shared<boost::shared_mutex>();
            valueAccess.insert(std::pair<Key, boost::shared_ptr<boost::shared_mutex> >(k, mtx));
            m.insert(std::pair<Key,Value>(k,v));
        }
    }

    Value get(Key k)
    {
        boost::shared_lock<boost::shared_mutex> lock(schemaAccess);
        return m.at(k);
    }

    bool get(Key k, Value& v)
    {
        boost::shared_lock<boost::shared_mutex> lock(schemaAccess);
        if(m.find(k) != m.end()){
            v = m.at(k);
            return true;
        }
        return false;
    }


private:
    std::map<Key, Value> m;

    std::map<Key, boost::shared_ptr<boost::shared_mutex> > valueAccess;
    boost::shared_mutex schemaAccess;

    //http://stackoverflow.com/questions/36468270/how-to-use-a-boostmutex-as-the-mapped-type-in-stdmap/36469809?noredirect=1#comment60552035_36469809
};

以下用法:

ConcurrentMap<int, cv::Mat> mapper;

void worker1(){
    int counter = 0;
    while(1){
        boost::this_thread::sleep(boost::posix_time::milliseconds(5));
        cv::Mat img = cv::Mat(1920,1080, CV_64F);
        cout << "w1" << counter++ << endl;
        mapper.set(0,img);
    }
}

void worker2(){
    int counter = 0;
    while(1){
        boost::this_thread::sleep(boost::posix_time::milliseconds(5));
        cout << "w2" << counter++ << endl;
        cv::Mat img;
        if(!mapper.get(0, img)) continue ;
        cout << img.rows << endl;
        cout << "done" << endl;
    }
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "loading_area_concat");


    boost::thread worker1_;
    boost::thread worker2_;
    worker1_ = boost::thread(worker1);
    worker2_ = boost::thread(worker2);

    ros::spin();

    return 0;
}

*** Error in `/home/raaj/catkin_ac2/devel/lib/loading_area/loading_area_concat': free(): invalid pointer: 0x00007fb71c000078 ***

然而,我的代码一直在崩溃&#34; get&#34;参与第二个主题。如果我发表评论&#34; get&#34;有用。不管怎样,或者如果我完全锁定下面的get代码,它可以工作:

    boost::upgrade_lock<boost::shared_mutex> schemaLock(schemaAccess);
    boost::upgrade_to_unique_lock<boost::shared_mutex> schemaUniqueLock(schemaLock);

我似乎无法理解为什么会这样。当我在哈希映射中写入该键时,我将其锁定,当我正在阅读它时,作者不应该进入并破坏内存是吗?

1 个答案:

答案 0 :(得分:0)

您是否也尝试锁定get()并检查问题是否消失了?从高级别的角度来看,如果没有写入,则只需要锁定读取。如果有一些线程写入该位置,则读取也需要受到保护,否则存在竞争条件(get()可能最终使用不再有效的东西)。

好的,我看到get()中的锁定解决问题的编辑,所以看起来确实如此。

问题是,不需要锁定并发读取不变的内存。但是如果有并发写入,那么读取也需要被锁定(反对写入,否则可能会发生各种奇怪的事情,如部分/交错读取等)。