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

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.
 
K

Kai-Uwe Bux

wolverine said:
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
 
W

wolverine

Kai-Uwe Bux said:
wolverine said:
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.
 
C

Chris

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.
Kai-Uwe Bux said:
wolverine said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top