I've actually heard claims both ways. I suspect they are not, but I wanted to get the topic settled.
-
2See also http://stackoverflow.com/questions/54188/are-c-reads-and-writes-of-an-int-atomic – sth Jan 19 '12 at 01:09
-
1C++ wouldn't have needed `atomic
` if that was the case :) – Hans Passant Jan 19 '12 at 01:31
2 Answers
C++03 does not know about the existance of threads, therefore the concept of atomicity doesn't make much sense for C++03, meaning that it doesn't say anything about that.
C++11 does know about threads, but once again doesn't say anything about the atomicity of assigning pointers. However C++11 does contain std::atomic<T*>
, which is guaranteed to be atomic.
Note that even if writing to a raw pointer is atomic on your platform the compiler is still free to move that assingment around, so that doesn't really buy you anything.
If you need to write to a pointer which is shared between threads use either std::atomic<T*>
(or the not yet official boost::atomic<T*>
, gccs atomic intrinsics or windows Interlocked*) or wrap all accesses to that pointer in mutexes.

- 19,595
- 4
- 60
- 78
-
You can get boost.atomic here: http://www.chaoticmind.net/~hcb/projects/boost.atomic/doc/index.html – Adi Shavit Feb 07 '12 at 14:49
The C++ norm does not define specific threading behavior. Depending on the compiler and the platform, the pointer assignment may or may not be atomic.

- 846
- 9
- 30
-
10C++11 *does* define a threading behavior and the associated memory model. – J.N. Jan 19 '12 at 01:29
-
1@Frizlab and J.N. you are both correct. As saying Porsche is a sport car is a correct but imprecise, as not every model is a sport car :) – mloskot Nov 22 '12 at 11:31