Is the operation "++" an atom in C?

I am trying to determine if there is such an operator:

++value; //assuming "value" is a **global** variable 

- atomic operation.

I need to know if this calculation can be interrupted by an interrupt service that writes the same global variable.

+2
source share
1 answer

On objects without an atomic type, the standard never defines ++ as an atomic operation.

C11 defines atomic types in stdatomic.h. If you have an atomic type object, the postfix and prefix ++ operators will define the atomic operation as a read-modify-write operation with memory_order_seq_cst semantics of the memory order.

You can also use atomic_fetch_add () if atomic increment is required.

+2
source

Source: https://habr.com/ru/post/1240954/


All Articles