By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,367 Members | 1,730 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,367 IT Pros & Developers. It's quick & easy.

will an iterator to a map becomes invalid when an element is inserted into the map

P: n/a
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.

Jul 31 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
wolverine wrote:
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.
[code snipped]

A fully executed insertion into a map does not invalidate iterators. A fully
executed deletion from a map only invalidates iterators refering to the
deleted object. These are guarantees provided by the standard (well,
without the stuff about fully executed: the standard does not say anything
about threading; but you can take the provisions of the standard as
postconditions for the operations it describes).
Best

Kai-Uwe Bux
Jul 31 '06 #2

P: n/a

Kai-Uwe Bux wrote:
wolverine wrote:
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.
[code snipped]

A fully executed insertion into a map does not invalidate iterators. A fully
executed deletion from a map only invalidates iterators refering to the
deleted object. These are guarantees provided by the standard (well,
without the stuff about fully executed: the standard does not say anything
about threading; but you can take the provisions of the standard as
postconditions for the operations it describes).
Best

Kai-Uwe Bux
Hi
Thanks for clearing my doubts on the topic. As a novice in stl ,i
am finding this group very helpful and thinks there are a lot of people
in this world willing to help others.

Jul 31 '06 #3

P: n/a
The C++ standard as far as I know does not say anything on the STL
collection classes as multithreading - simple because they are designed
for a single threaded environment. As a good rule, when using the STL
collection classes within a multithread environment, all reads and
writes should be protected by some sort of lock.
Many implementations may make certain guarantees regarding iterator
invalidation on the same thread but some STl implementations may not
adhere strictly to the standard.
Of course it you initialize the collections a priori and only do reads
across threads then you dont need any synchronization.

wolverine wrote:
Kai-Uwe Bux wrote:
wolverine wrote:
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.
[code snipped]

A fully executed insertion into a map does not invalidate iterators. A fully
executed deletion from a map only invalidates iterators refering to the
deleted object. These are guarantees provided by the standard (well,
without the stuff about fully executed: the standard does not say anything
about threading; but you can take the provisions of the standard as
postconditions for the operations it describes).
Best

Kai-Uwe Bux

Hi
Thanks for clearing my doubts on the topic. As a novice in stl ,i
am finding this group very helpful and thinks there are a lot of people
in this world willing to help others.
Jul 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.