Versions Compared

Key

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

...

Code Block
bgColor#ccccff
#include <signal.h>

volatile sig_atomic_t i;

void handler(int signum) {
  i = 0;
}

int main(void) {
  i = 1;
  signal(SIGINT, handler);
  while (i) {
   /* do something */
  }
  return 0;
}

Wiki MarkupThe {{sig_atomic_t}} type is the integer type of an object that can be accessed as an atomic entity, even in the presence of asynchronous interrupts. The type of {{sig_atomic_t}} is [implementation-defined|BB. Definitions#implementation-defined behavior], although there are constraints. Only assign integer values from 0 through 127 to a variable of type {{sig_atomic_t}} to be fully portable (see \[[SIG31-C. Do not access or modify shared objects in signal handlers]\]).

Risk Assessment

Failing to use the volatile qualifier can result in race conditions in asynchronous portions of the code, causing unexpected values to be stored and leading to possible data integrity violations.

...