Do I need to block STL containers to read their data?

I am writing a multithreaded server that contains 5 or 6 global data structures (maps, vectors, etc.), and I'm trying to figure out if I need to store mutexes for certain data in order to read values ​​from maps or vectors, or if only save mutex when i'm going to change data / add new elements.

+4
source share
1 answer

You need to synchronize access to the container if multiple threads are accessing the container, and at least one of these threads modifies the contents of the container. If none of the threads ever changes the contents of the container, you do not need to synchronize access with it.

[Note that the C ++ language standard does not mention streams (at least not yet), so it is not required that containers can be used from multiple streams. However, what I said above is true, at least for all major implementations of the standard library, and is a requirement in the upcoming version of C ++ 0x for the C ++ language standard.]

+5
source

All Articles