Reputation: 10539
Is there any guarantee that the assignment of a pointer variable is atomic?
struct S {char a; int* p;};
S * p1 = new S;
S * p2 = new S;
p1 = p2;
Upvotes: 1
Views: 1084
Reputation: 5002
In C++03 there are no such guaranties, because the language in not aware of threads. However on Win32 pointer assignment is guaranteed to be atomic.
Simple reads and writes to properly-aligned 32-bit variables are atomic operations. In other words, you will not end up with only one portion of the variable updated; all bits are updated in an atomic fashion. However, access is not guaranteed to be synchronized. If two threads are reading and writing from the same variable, you cannot determine if one thread will perform its read operation before the other performs its write operation.
Simple reads and writes to properly aligned 64-bit variables are atomic on 64-bit Windows. Reads and writes to 64-bit values are not guaranteed to be atomic on 32-bit Windows. Reads and writes to variables of other sizes are not guaranteed to be atomic on any platform.
In C++11 there are no such guarantees either, unless std::atomic is used.
Upvotes: 2