473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Testing if an iterator is invalid ?

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 19 '08 #1
8 2742
On Jul 19, 7:12 pm, khalid...@gmail.com 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.


Jul 19 '08 #2
On Jul 20, 2:24 am, puzzlecracker <ironsel2...@gmail.comwrote:
On Jul 19, 7:12 pm, khalid...@gmail.com 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<intso 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 19 '08 #3
On 7ÔÂ20ÈÕ, ÉÏÎç7ʱ38·Ö, khalid...@gmail.com wrote:
On Jul 20, 2:24 am, puzzlecracker <ironsel2...@gmail.comwrote:
On Jul 19, 7:12 pm, khalid...@gmail.com 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<intso 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
Jul 20 '08 #4
dean wrote:
On 7ÔÂ20ÈÕ, ÉÏÎç7ʱ38·Ö, khalid...@gmail.com 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.
Jul 20 '08 #5
Sam
puzzlecracker writes:
On Jul 19, 7:12 pm, khalid...@gmail.com 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.
Bullshit.

Once an element is deleted from a collection, the element no longer exist.
It cannot be set to NULL, 3.1415926, or any other value, after it is removed
from a std::set.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkiClowACgkQx9p3GYHlUOIWAACfYY3rJ/tyKGv7NIOysFWKUDyU
L5UAn0W+FcSi1Ad1wfWq2VFxBMgsnxWJ
=+01g
-----END PGP SIGNATURE-----

Jul 20 '08 #6
Sam
kh*******@gmail.com writes:
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.

You will need to implement some other synchronization mechanism.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkiClt8ACgkQx9p3GYHlUOLWgQCdHL0OE4U4z2 d6Pmm534DkxKTQ
cmAAn2CcCzdpXE2NW7yLSgYeQL/5DAfP
=e7qK
-----END PGP SIGNATURE-----

Jul 20 '08 #7
On Jul 20, 1:12 am, khalid...@gmail.com 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:ja*********@gmail.com
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
Jul 20 '08 #8
On Jul 20, 4:07 am, Ian Collins <ian-n...@hotmail.comwrote:
dean wrote:
On 7ÔÂ20ÈÕ, ÉÏÎç7ʱ38·Ö, khalid...@gmail.com 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 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: steven | last post by:
Hi, Anyone was using pmock for unit testing with python? I met a problem and hope someone to help me. For short, the pmock seems can not mock a iterator object. For example, the tested...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
1
by: Gonzalo Aguirre | last post by:
hi!. i did this method that should perform as many iterations as Sigma element has (in the example just two: a, b), for each state (outer bucle). vector<_t_> * Mealy::to_deltaMoore() {...
7
by: PengYu.UT | last post by:
I'm wondering is the standard defined behavior of past bound iterator. In the following example it seems that afer first "--it", it point to -1 index. I'm wondering if it is true independent of...
3
by: wolverine | last post by:
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...
12
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back...
3
by: JurgenvonOerthel | last post by:
I want to replace one element in a list<stringby a list<stringand I need to obtain an iterator to the first element in the inserted list. In code: void...
4
by: thinktwice | last post by:
std::vector<CStringvec; vec::iterator iter; i can initiaze the iterator in vc6 like this $B!'(B iter = NULL, but it compile failed in vc2005, it tells me there is no acceptable conversion.
8
by: Bo Yang | last post by:
Hi, Today, I make some test on the C++ STL iterators of set containers with GNU g++ compiler. The code is: #include <set> #include <iostream> using namespace std; int main(int argc, char...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.