I would like a summary of what exactly is thread safe in C++ both according to the current standard and C++0x as well as in practice (generally speaking, but also in my case with gcc 4.5.1).
For STL containers, my understanding is that thread safety is not guaranteed according to the current standard. Is it true though that in practice they are thread safe for single writer, multiple reader usage (on gcc and probably most modern compilers)? Is this guaranteed by C++11?.
What about POD types? I know that the standard guarantees nothing, but I’ve been told that in practice, all are thread safe for reading and writing. Of course even something simple like the increment operator may still need synchronization since there could be multiple reads and writes.
I’m primarily interested in the answers, but the why behind the answers would be appreciated to.
The current standard doesn’t mention threading at all, in any respect. In practice, the standard containers provide thread-safe reading, but require synchronization for writing.
C++ 0x doesn’t talk much (at all?) specifically about containers with respect to thread safety/sharing, but does talk about assignments and such. In the end, it comes out pretty much the same though — even though the object is in a container, you’re reading/writing data, and you have to synchronize when/if at least one thread may modify the data.
POD data doesn’t really change much: modifications will require synchronization as a general rule. There’s usually some subset of data types for which operations are normally atomic, but the members of that subset vary by platform. It’ll typically include types up to the native word size of the hardware allocated with “natural” alignment; anything else is open to a lot more question.