W
wolverine
Hi
I am accessing a map from inside threads. There is a chance
that an element is inserted into the map, from inside any thread. Since
i don't know about thread safety of stl implementation i am using , i
use mutex for thread safety. Now comes the question. Please answer this
question assuming that i am not using a thread safe stl version.
I obtain an iterator to the map from inside the critical
section(protected by mutex). If i use the iterator ,outside the region
protected by mutexes , is there any problem like iterator becoming
invalid. There are chances that other thread may enter the critical
section and insert an element into the map. If this happens before i
use the iterator, will the iterator become invalid.
Thanks in advance.
The sampe code is as below
if (pthread_mutex_lock(&mutexCheckDuplicate))
{
LOG1(ERROR, "ERROR IN LOCKING MUTEX - mutexCheckDuplicate");
}
sURLMapItr itr = mURLs.find(sURL->getAbsoluteURL());
if( itr != mURLs.end())
{
if (pthread_mutex_unlock(&mutexCheckDuplicate))
{
LOG1(ERROR, "ERROR IN UNLOCKING MUTEX - mutexCheckDuplicate");
}
delete sURL;
//the QUESTION is about this statement
sURL = itr->second;
}
else
{
//insert into the map if it is not present
mURLs.insert(make_pair(sURL->getAbsoluteURL(), sURL));
if (pthread_mutex_unlock(&mutexCheckDuplicate))
{
LOG1(ERROR, "ERROR IN UNLOCKING MUTEX - mutexCheckDuplicate");
}
}
Regards
Kiran.
I am accessing a map from inside threads. There is a chance
that an element is inserted into the map, from inside any thread. Since
i don't know about thread safety of stl implementation i am using , i
use mutex for thread safety. Now comes the question. Please answer this
question assuming that i am not using a thread safe stl version.
I obtain an iterator to the map from inside the critical
section(protected by mutex). If i use the iterator ,outside the region
protected by mutexes , is there any problem like iterator becoming
invalid. There are chances that other thread may enter the critical
section and insert an element into the map. If this happens before i
use the iterator, will the iterator become invalid.
Thanks in advance.
The sampe code is as below
if (pthread_mutex_lock(&mutexCheckDuplicate))
{
LOG1(ERROR, "ERROR IN LOCKING MUTEX - mutexCheckDuplicate");
}
sURLMapItr itr = mURLs.find(sURL->getAbsoluteURL());
if( itr != mURLs.end())
{
if (pthread_mutex_unlock(&mutexCheckDuplicate))
{
LOG1(ERROR, "ERROR IN UNLOCKING MUTEX - mutexCheckDuplicate");
}
delete sURL;
//the QUESTION is about this statement
sURL = itr->second;
}
else
{
//insert into the map if it is not present
mURLs.insert(make_pair(sURL->getAbsoluteURL(), sURL));
if (pthread_mutex_unlock(&mutexCheckDuplicate))
{
LOG1(ERROR, "ERROR IN UNLOCKING MUTEX - mutexCheckDuplicate");
}
}
Regards
Kiran.