我在使用线程时遇到以下代码问题。
我在 Microsoft 网站上读到,附加到concurrent_vector
不会与迭代器混淆,所以我没有mutex
在操作期间提供和find_if
。
所以我收到的错误是“访问冲突”
我有 6 个线程同时运行。我应该把它包起来mutex
吗?是不是需要一个。我对 C++ 相当陌生。
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();
}
);
}
}