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

Discussion in 'C++' started by wolverine, Jul 31, 2006.

  1. wolverine

    wolverine Guest

    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.
     
    wolverine, Jul 31, 2006
    #1
    1. Advertising

  2. wolverine

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Jul 31, 2006
    #2
    1. Advertising

  3. wolverine

    wolverine Guest

    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.
     
    wolverine, Jul 31, 2006
    #3
  4. wolverine

    Chris Guest

    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.
     
    Chris, Jul 31, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    3
    Views:
    1,775
    Peter van Merkerk
    Oct 5, 2004
  2. Replies:
    9
    Views:
    1,048
    Juha Nieminen
    Aug 22, 2007
  3. Replies:
    3
    Views:
    390
  4. Stef Mientki

    and becomes or and or becomes and

    Stef Mientki, May 22, 2011, in forum: Python
    Replies:
    9
    Views:
    336
    Chris Angelico
    May 28, 2011
  5. Jim Anderson

    problem with iterator (map iterator)

    Jim Anderson, Jan 10, 2014, in forum: C++
    Replies:
    3
    Views:
    159
    Luca Risolia
    Jan 13, 2014
Loading...

Share This Page