...
| Code Block | ||
|---|---|---|
| ||
#include <signal.h>
volatile sig_atomic_t i;
void handler() {
i = 0;
}
int main(void) {
signal(SIGINT, handler);
i = 1;
while (i) {
/* do something */
}
}
|
The sig_atomic_t type is the (possibly volatile-qualified) 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, although there are bounding constraints. Only assign integer values from 0 through 127 to a variable of type sig_atomic_t to be fully portable.
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, leading to possible data integrity violations.
...