I ‘ve declared a vector as
typedef std::vector< unsigned int > SampleList;
and declared Samplist type member variable in a class.
I am accessing this vector from another class with multiple threads.
I am adding , deleting , Reading values values from different threads. I am reading this value frequently like the following.
SampleList* listSample;
listSample= ptr->GetList();
while(true)
{
SampleList::iterator itrSample;
itrSample = listSample->begin();
unsigned int nId = 0;
for ( ; itrSample < listRoundRobinSensor->end(); ++itrSample )
{
nId =(unsigned int) *itrSample ;
}
}
The value for itrSample becomes junk value like 4261281277.
I tried to guard this list with critical secion. Still I got this issue. Can you suggest and solution. It will be very helpful to me.
As soon as somebody adds or deletes a member of the vector, you iterator becomes invalid.
Especially if elements are added, the internal buffer might have to be reallocated. But also if objects are deleted, the end() is moving and you might miss it.
You must have a lock to protect the vector while iterating over it.