Testing if an iterator is invalid ?

K

khalid302

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 ?
 
P

puzzlecracker

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.
 
K

khalid302

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
 
D

dean

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
 
I

Ian Collins

dean said:
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.
 
J

James Kanze

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.
 
K

khalid302

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,475
Latest member
NovellaSce

Latest Threads

Top