Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   will an iterator to a map becomes invalid when an element is inserted into the map (http://www.velocityreviews.com/forums/t455783-will-an-iterator-to-a-map-becomes-invalid-when-an-element-is-inserted-into-the-map.html)

wolverine 07-31-2006 11:11 AM

will an iterator to a map becomes invalid when an element is inserted into the map
 
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.


Kai-Uwe Bux 07-31-2006 11:24 AM

Re: will an iterator to a map becomes invalid when an element is inserted into the map
 
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

wolverine 07-31-2006 11:47 AM

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

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.


Chris 07-31-2006 12:24 PM

Re: will an iterator to a map becomes invalid when an element is inserted into the map
 
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.




All times are GMT. The time now is 01:00 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.