Testing if an iterator is invalid ?

Discussion in 'C++' started by khalid302@gmail.com, Jul 20, 2008.

  1. Guest

    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 ?
    , Jul 20, 2008
    #1
    1. Advertising

  2. On Jul 19, 7:12 pm, 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.
    puzzlecracker, Jul 20, 2008
    #2
    1. Advertising

  3. Guest

    On Jul 20, 2:24 am, puzzlecracker <> wrote:
    > On Jul 19, 7:12 pm, 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
    , Jul 20, 2008
    #3
  4. dean Guest

    On 7ÔÂ20ÈÕ, ÉÏÎç7ʱ38·Ö, wrote:
    > On Jul 20, 2:24 am, puzzlecracker <> wrote:
    >
    > > On Jul 19, 7:12 pm, 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
    dean, Jul 20, 2008
    #4
  5. Ian Collins Guest

    dean wrote:
    > On 7ÔÂ20ÈÕ, ÉÏÎç7ʱ38·Ö, 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.
    Ian Collins, Jul 20, 2008
    #5
  6. James Kanze Guest

    On Jul 20, 1:12 am, 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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 20, 2008
    #6
  7. Guest

    On Jul 20, 4:07 am, Ian Collins <> wrote:
    > dean wrote:
    > > On 7ÔÂ20ÈÕ, ÉÏÎç7ʱ38·Ö, 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.
    , Jul 20, 2008
    #7
    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. Hendrik Maryns
    Replies:
    18
    Views:
    1,404
  2. greg
    Replies:
    6
    Views:
    447
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    634
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,137
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,027
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page