views:

128

answers:

4

I have a global object holding several c++ std::map objects. This object is supposed to be read only in a multithreaded environment. But i'm not sure whether there is any write operation when a C++ std::map object is being read within the implementation of std::map. The IDE is Visual Studio 2008. Should I provide some synchronization mechanisms for read operations?

+1  A: 

There is at least one "portable" problem, in that if you're using operator[] to read, it is not necessarily a read only operation in that if the item doesn't exist yet in the map, it will be created. Beyond that, it becomes a matter of the specific implementation.

Logan Capaldo
"operator[]" is what i'm using. I'll fix it.
lifengzhong
+3  A: 

Yes this will be OK provided nobody is writing to the map. See here for full details.

http://stackoverflow.com/questions/1846186/thread-safety-of-stdmap-for-read-only-operations

Steve Townsend
+1 for the link.
JoshD
+1 for being the only correct answer... yikes!
Tony
@Tony: can you be more specific as to what's wrong with the other answers?
Eugen Constantin Dinca
@Eugen: Login says "matter of specific implementation", you "check...your...implementation", Josh "probably ... unsafe". That's FUD. Of course you need to ensure some synchronisation after the last update, but after that concurrent reading of any STL container is - as Steve says - safe.
Tony
@Tony for a while there were several implementations of std::string out there that weren't thread safe because they used COW in a non-thread safe manner. You really _do_ need to check your implementation, as far as the C++ standard is concerned, before C++0x threads weren't even imagined.
Logan Capaldo
@Logan: good historic counter-example :-). So, Steve's right in saying this about std::map, but I'm wrong in generalising to all STL containers... VC++6 and GCC<=3.0 users beware. And while C++ Standard doesn't comment on this specifically, is it not fair to say that SGI's policies reflect (set?) the benchmark that all implementations were expected to live up to - http://www.sgi.com/tech/stl/thread_safety.html - hence the fixes to std::string? In threading, stipulations aren't in the Standard, but the reasonable expectations of the environment have been well understood for a long time.
Tony
A: 

If you use the subscript operator, then no. There's a chance that the subscript operator (map[1]) will alter the map's structure.

However, if you use the find method with const_iterators, it is reasonable safe. Just remember that all those iterators may become invalid if the map is altered.

With that said, it is still probably an unsafe idea.

JoshD
A: 

See here for the specifics of the std library that ships with VC++ 2008:

A single object is thread safe for reading from multiple threads. For example, given an object A, it is safe to read A from thread 1 and from thread 2 simultaneously.

There are no rules regarding the std containers' thread safety, it's up to the library developers. So always check the documentation of your specific std implementation.

Eugen Constantin Dinca