并行化STL列表擦除功能时的分段错误

时间:2015-07-07 17:30:43

标签: stl segmentation-fault openmp erase-remove-idiom

我正在尝试使用Open MP并行化我的C ++代码但是我遇到了Segmentation故障,串行版本运行正常。我使用了gdb调试器,我知道在擦除STL列表中的一个元素时发生了错误。

在下面的代码中,我有一个向量,其中包含带ID的粒子对(boxID1和boxID2),每个粒子都有一个与之关联的实际联系人列表。然后我添加并删除粒子在给定列表中是否接触的情况。

有没有办法在删除时避免此错误?

        Contact newcontact;
        bool isPresentIntheList;
        list<Contact>::iterator it;
        #pragma omp parallel for private(newcontact, isPresentIntheList, it)
        for(long int i=0; i<overlapVector.size(); i++)
        {
            isPresentIntheList = false;
            it = (*particlesPointerVector)[overlapVector[i].boxID2]->ActualContactList.begin();//iterator for Actual Contact List of the particle with ID = overlapVector[i].boxID2
            if((*particlesPointerVector)[overlapVector[i].boxID2]->ActualContactList.size() != 0) //if ACL size doesn't have a size = 0
            {
                for(long int j=0; it!=(*particlesPointerVector)[overlapVector[i].boxID2]->ActualContactList.end(); j++)
                {
                    if(it->particlei->ID == overlapVector[i].boxID1)
                    {
                        isPresentIntheList = true;
                    }

                    if(isPresentIntheList)
                    {
                        if(IsContact(*it, timestep))//checking if the overlapping pair is in contact or not
                        {
                            it++;//do nothing that means overlapping pair is in contact
                        }

                        else//remove that pair from ACL if it is not in contact
                        {

                            {
                                newcontact.particlei = (*it).particlei;
                                newcontact.particlej = (*it).particlej;
                                /*ERROR occurring here*/
                                it = (*particlesPointerVector)[overlapVector[i].boxID2]->ActualContactList.erase(it);//no it++ required as it automatically points to the next container after erasing

                            }

                        }

                    }
                    else if(!isPresentIntheList)
                        it++;
                }
        }

0 个答案:

没有答案