STL map
Apr 10, 2009 at 6:56am UTC
I have written a multi threaded code. Where I am reading values into a map and then reading it just after that.
Its running absolutely fine but in some cases, its not giving the value I have written into it.
Here is the code snippet
1 2 3
map<int , int > log;
log[key] = key;
assert(log[key] == key);
this assertion is failing in some cases.
I have not changed this log anywhere else. So, I don't understand what is the problem. Is it possible that stl map is not working properly??
Apr 10, 2009 at 7:11am UTC
http://www.cprogramming.com/tutorial/stl/stlmap.html
Apr 10, 2009 at 7:18am UTC
Didn't get much info in the above link.. :(.
Can someone tell me if STL map supports multithreading. I haven't used lock while many threads trying to add items into the map. Can that be a problem??
Apr 10, 2009 at 7:28am UTC
HUH....
This is really confusing... some people say STL:map is thread safe and some says its not... :(
Whats actually the answer?
Apr 10, 2009 at 10:17am UTC
STL is not thread safe. But mostly you can avoid race conditions as there is no static data in STL. But otherwise you need to synchronize your code if you have many threads.
Apr 10, 2009 at 1:40pm UTC
Thanks for the help.. :)
Apr 10, 2009 at 3:03pm UTC
Nothing in STL is threadsafe for any number of threads. You are lucky the above code doesn't just seg fault.
Apr 10, 2009 at 3:21pm UTC
Yes, you will need to use mutexes or locks in order to prevent multiple threads from accessing the data at the same time.
Topic archived. No new replies allowed.