Hi all,
In a couple of occasions I lost quite a lot of time finding a bug that was throwing a segmentation fault due to a race condition.
It happened to me in OpenMP parallel processes, when accessing ProcessInfo[SOME_VARIABLE] or GetProperties()[SOME_OTHER_VARIABLE]. The memory was not allocated for those variables and different threads attempted to allocate it at the same time. Some methods like InitializeSolutionStep don't accept the ProcessInfo as const, so one must use some tricks -like casting to const- to avoid this problem. Then I thought that we could work on the DataValueContainer to make it threadsafe. Does it make sense to add a 'critical' in the GetValue method? Written next:
template<class TDataType> TDataType& GetValue(const Variable<TDataType>& rThisVariable)
{
typename ContainerType::iterator i;
if ((i = std::find_if(mData.begin(), mData.end(), IndexCheck(rThisVariable.Key()))) != mData.end())
return *static_cast<TDataType*>(i->second);
#pragma omp critical
{
mData.push_back(ValueType(&rThisVariable,new TDataType(rThisVariable.Zero())));
}
return *static_cast<TDataType*>(mData.back().second);
}