Delete a list item from a separate thread safe?



I've got a list that will be accessed by multiple threads. The list will be
protected by a critical section.

However, I'm wondering about this case. Say one thread is using an iterator
to step through the list. The stepping from one list item to the next will
be in a critical section (but not while using the list item).

Say the loop is on the 3rd item when a separate thread deletes the 3rd item.
The deletion will be done inside the same critical section.

Essentially this means thread 1 will be processing item 3 while it is being
deleted by thread 2. Then thread 1 will increment the iterator to step from
item 3 (now deleted) to item 4 (now 3).

Will the iterator in thread 1 still step correctly if the item it's stepping
from has been deleted?

Bruce.


.



Relevant Pages

  • Re: Delete a list item from a separate thread safe?
    ... The stepping from one list item ... Then thread 1 will increment the iterator ... This will exhibit undefined behavior even before you try to increment ... The only action safe to perform on an invalid iterator ...
    (microsoft.public.vc.stl)
  • Re: Delete a list item from a separate thread safe?
    ... The deletion will be done inside the same critical section. ... Then thread 1 will increment the iterator to step from ... Will the iterator in thread 1 still step correctly if the item it's stepping ... Visual C++ MVP ...
    (microsoft.public.vc.stl)
  • Re: STL Vector: Unexpected behavior
    ... As erase returns an iterator to the element following the one erased one might use localIterator = localVector.erase(..... ... But you must remove the incrementing of the iterator from the for statement or you skip every second element. ... middle and then increment 'index' you skipped an element. ...
    (microsoft.public.vc.stl)
  • Re: Some more questions...
    ... An iterator can just be a pointer - in that case you already know what ... happens when you increment it past the end. ... Knowing how iterators work in Java has certainly ...
    (comp.lang.cpp)
  • Re: deleting entries in a list
    ... > that iterator becomes invalidated. ... > that invalid iterator. ... > don't increment 'it', assign it the return value from ...
    (comp.lang.cpp)