Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Testing if an iterator is invalid ?

Reply
Thread Tools

Testing if an iterator is invalid ?

 
 
khalid302@gmail.com
Guest
Posts: n/a
 
      07-19-2008
I'm writing a multi-threaded application where one thread waits for a
certain std::set element to be deleted by another thread.

The waiting thread already has an iterator pointing at the element
that has to be deleted. After the other thread deletes this element,
the iterator will become invalid.

Is there a way to know if a certain iterator is invalid ?
 
Reply With Quote
 
 
 
 
puzzlecracker
Guest
Posts: n/a
 
      07-19-2008
On Jul 19, 7:12 pm, (E-Mail Removed) wrote:
> I'm writing a multi-threaded application where one thread waits for a
> certain std::set element to be deleted by another thread.
>
> The waiting thread already has an iterator pointing at the element
> that has to be deleted. After the other thread deletes this element,
> the iterator will become invalid.
>
> Is there a way to know if a certain iterator is invalid ?



You can assign NULL to the deleted element, and have the waiting
thread check for null-ness after the waiting time.




 
Reply With Quote
 
 
 
 
khalid302@gmail.com
Guest
Posts: n/a
 
      07-19-2008
On Jul 20, 2:24 am, puzzlecracker <(E-Mail Removed)> wrote:
> On Jul 19, 7:12 pm, (E-Mail Removed) wrote:
>
> > I'm writing a multi-threaded application where one thread waits for a
> > certain std::set element to be deleted by another thread.

>
> > The waiting thread already has an iterator pointing at the element
> > that has to be deleted. After the other thread deletes this element,
> > the iterator will become invalid.

>
> > Is there a way to know if a certain iterator is invalid ?

>
> You can assign NULL to the deleted element, and have the waiting
> thread check for null-ness after the waiting time.


It's an std::set<int> so I'll assign a -1 instead of a NULL.

The waiting thread can then remove that element. The problem now
becomes, there could more than one thread waiting for the same element
to be deleted, which one of them should do the actual deletion.

There isn't a way to check if an iterator is invalid, right ?

Thanks for your idea
 
Reply With Quote
 
dean
Guest
Posts: n/a
 
      07-20-2008
On 7月20日, 上午7时38分, (E-Mail Removed) wrote:
> On Jul 20, 2:24 am, puzzlecracker <(E-Mail Removed)> wrote:
>
> > On Jul 19, 7:12 pm, (E-Mail Removed) wrote:

>
> > > I'm writing a multi-threaded application where one thread waits for a
> > > certain std::set element to be deleted by another thread.

>
> > > The waiting thread already has an iterator pointing at the element
> > > that has to be deleted. After the other thread deletes this element,
> > > the iterator will become invalid.

>
> > > Is there a way to know if a certain iterator is invalid ?

>
> > You can assign NULL to the deleted element, and have the waiting
> > thread check for null-ness after the waiting time.

>
> It's an std::set<int> so I'll assign a -1 instead of a NULL.
>
> The waiting thread can then remove that element. The problem now
> becomes, there could more than one thread waiting for the same element
> to be deleted, which one of them should do the actual deletion.
>
> There isn't a way to check if an iterator is invalid, right ?
>
> Thanks for your idea


I think you should choose some other thread synchronization mechanism
rather than checking the iterator, mutex may be a good choice
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-20-2008
dean wrote:
> On 7月20日, 上午7时38分, (E-Mail Removed) wrote:
>>
>> The waiting thread can then remove that element. The problem now
>> becomes, there could more than one thread waiting for the same element
>> to be deleted, which one of them should do the actual deletion.
>>
>> There isn't a way to check if an iterator is invalid, right ?
>>
>> Thanks for your idea

>
> I think you should choose some other thread synchronization mechanism
> rather than checking the iterator, mutex may be a good choice


Better to a synchronisation object such as a condition variable or
semaphore.

Never use STL objects for thread synchronisation.

--
Ian Collins.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-20-2008
On Jul 20, 1:12 am, (E-Mail Removed) wrote:
> I'm writing a multi-threaded application where one thread waits for a
> certain std::set element to be deleted by another thread.


> The waiting thread already has an iterator pointing at the element
> that has to be deleted. After the other thread deletes this element,
> the iterator will become invalid.


> Is there a way to know if a certain iterator is invalid ?


No.

The suggestions of assigning NULL to the iterator (which
probably won't compile) or assigning something through it (which
is undefined behavior, and will likely lead to a core dump) are
just bullshit. Ian is the only one who really got this right:
you must use a special synchronization object, supported by your
OS. Under Unix, pthread_cond_t; under Windows, I don't know.
Or better yet, use the conditional object in Boost threads.

Without external synchronization by the OS (or perhaps some
platform specific assembler code), you have undefined behavior.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orient閑 objet/
Beratung in objektorientierter Datenverarbeitung
9 place S閙ard, 78210 St.-Cyr-l'蒫ole, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
khalid302@gmail.com
Guest
Posts: n/a
 
      07-20-2008
On Jul 20, 4:07 am, Ian Collins <(E-Mail Removed)> wrote:
> dean wrote:
> > On 7月20日, 上午7时38分, (E-Mail Removed) wrote:

>
> >> The waiting thread can then remove that element. The problem now
> >> becomes, there could more than one thread waiting for the same element
> >> to be deleted, which one of them should do the actual deletion.

>
> >> There isn't a way to check if an iterator is invalid, right ?

>
> >> Thanks for your idea

>
> > I think you should choose some other thread synchronization mechanism
> > rather than checking the iterator, mutex may be a good choice

>
> Better to a synchronisation object such as a condition variable or
> semaphore.


I have been doing just that, however, I was using a single condition
variable for the whole std::set. So it was one of two ways:

Each time this single condition variable is broadcast, each waiting
thread calls set.find() for the element value its waiting for the
deletion of. But this means so many false set.find() calls.

Now I resorted to using a condition variable for each std:set element
and do a broadcast upon the deletion of this certain element.

However, if I was able to check for the validity of the iterator pre-
acquired by the waiting thread with find(). I wouldn't have had to
create multiple condition variables.

Thanks for your help

> Never use STL objects for thread synchronisation.
>
> --
> Ian Collins.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments