我正在尝试使用 Open MP 并行化我的 C++ 代码,但我遇到了分段错误,串行版本运行良好。我使用了 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++;
}
}