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 Markup
The {{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.  For more on {{sig_atomic_t}} please see \[[SIG31-C. Do not access or modify shared objects in signal handlers]\].

Another acceptable solution involves specifying the volatile keyword temporarily by using a typecast.

...