...
This section only includes an example for the addition of atomic integer types. For other operations, you can use tests similar to the precondition tests for two’s complement integers used for non-atomic integer types.
...
This noncompliant code example using atomic integers can result in unsigned integer overflow wrapping.
Code Block |
---|
atomic_int i; int ui1; /* Initialize i, ui1 */ atomic_fetch_add(&i, ui1); |
Compliant Solution
This compliant solution performs a post-condition test to ensure that the result of the unsigned addition operation to i
is not less than the operand ui1
.
Code Block |
---|
atomic_int i; int ui1; /* Initialize ui1, i */ atomic_fetch_add(&i, ui1); if (atomic_load(&i) < ui1) { /* handle error condition */ } |
Exceptions
INT30-EX1. Unsigned integers can exhibit modulo behavior (wrapping) only when this behavior is necessary for the proper execution of the program. It is recommended that the variable declaration be clearly commented as supporting modulo behavior and that each operation on that integer also be clearly commented as supporting modulo behavior.
...