Home > database >  Are atomic operations on non-atomic types in C atomic?
Are atomic operations on non-atomic types in C atomic?

Time:03-25

The C17 standard specifies a list of atomic operations. For example, an atomic read-write-modify operation on an atomic object of type A is defined in the standard as:

C atomic_fetch_add(volatile A *object, M operand);

But we can call atomic_fetch_add on non-atomic types:

static int x;
static int foo(void *arg) {
  atomic_fetch_add(&x, 3);
}

My question: is the above atomic_fetch_add operation on the non-atomic object x guaranteed to be atomic?

CodePudding user response:

on an atomic object of type A is defined in the standard

But we can call

is the above atomic_fetch_add operation on the non-atomic object x guaranteed to be atomic?

No. In the standard the behavior of the code you presented is not defined, there is no guarantee of any kind.

Real-life example, on clang you'll get an error.

  • Related