lundi 28 mars 2016

C++ concurrency::concurrent_vector

I am having issues with the following piece of code while using threads. I read on the microsoft site that appending to the concurrent_vector does not mess with iterators, so i did not provide and mutex for the duration of the find_if operation. So the error i am receiving is an "Access violation" I have 6 threads running concurrently. should i wrap this in a mutex? does it need one. I'm fairly new to c++. Thanks. G.

std::stringstream key;
key << "SearchString " << ID << ", " << "Options" << ", " << Date;
auto &it = std::find_if(m_spList.begin(), m_spList.end(), [&key]   (std::unique_ptr<IBaseObject>const &bo){return bo->ID() == key.str(); });
if (it != m_spList.end())
{
    while (it != m_spList.end())
    {
        ReplacePartResult* rpr = dynamic_cast<ReplacePartResult*>(it->get());
        if (rpr)
        {
            if (rpr->ReplaceItem)
            {
                replaceBOMID = rpr->BOMID > 0 ? rpr->BOMID : 0;
                if (_parentPart)
                {
                    _parentPart->TemplateBomID = rpr->BOMID;
                    _parentPart->Name = rpr->Name;
                    _parentPart->Description = rpr->Description;
                }
            }
        }
        it = std::find_if(++it, m_spList.end(), [&key](std::unique_ptr<IBaseObject>const &bo){return bo->ID() == key.str(); });
    }
}

Aucun commentaire:

Enregistrer un commentaire