You need two things to safely use an object simultaneously with two or more threads: atomicity of operations and guarantees of order.
Some people will pretend that on some platforms what you are trying to do here is safe, for example, operations on any type of int means that these platforms are atomic (even incrementing or whatever). The problem is that you do not necessarily have an order guarantee. Therefore, while you want to know that this particular variable will be available at the same time, the compiler does not. (And the compiler is right to assume that this variable will be used only one thread at a time: you do not want each variable to be considered as potentially separated. The performance consequences would be terrible.)
Therefore, do not use primitive types in this way. You have no guarantees from the language, and even if some platforms have their own guarantees (for example, atomicity), you have no way to tell the compiler that the variable is shared with C ++. Either use compiler extensions for atomic types, C ++ 0x atomic types, or library solutions (e.g. mutexes). And do not let the name fool you: to be truly useful, the atomic type must provide guarantees of order along with the atomicity that comes with the name.
source share