Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This compliant solution performs a postcondition test to ensure that the result of the unsigned addition operation to i is not less than the operand a.  However However, this code contains a race condition where i can be modified after the addition, but prior to the atomic load.  This  This solution is only compliant if i is guaranteed to only be access by a single thread.  See CON43 See CON08-C. Do not assume that a group of calls to independently atomic methods is atomic for more information.

 

Code Block
bgColor#ccccff
langc
#include <stdatomic.h>
 
atomic_uint i;

void func(unsigned int a) {
  atomic_fetch_add(&i, a);
  if (atomic_load(&i) < a) {
    /* Handle error condition */
  }
  /* ... */
}

Exceptions

...

...