Given the below code:
static int x;
static void f() {
for (int i = 0; i < 100; ++i)
atomic_fetch_add(&x, 3);
}
Further, assume that f
is called by two threads concurrently. Does C/C++ memory model guarantee that the result would always be 600
on all hardware platforms?
And what if I changed it to the below? Is the result still guaranteed to be 600
on all hardware platforms?
static atomic_int a_x;
static void f() {
for (int i = 0; i < 100; ++i)
a_x += 3;
}
Or the result is not guaranteed and I should never mix atomic operations with non-atomic types and vice versa?
PS: I used an int type here but my question applies to any type T
and _Atomic T
.