According to this site, one can use variables of type volatile sig_atomic_t
inside a signal handler. Now my question is, would for example something like the following code still be atomic and thus introduce no race conditions?
Assume that we are using a multicore processor (EDIT: running a multithreaded program). Does volatile sig_atomic_t
even work for multicore systems in the first place or should we use the atomic<unsigned int>
of C++11 for signal handlers on a multicore system (EDIT: running a multithreaded program)?
volatile sig_atomic_t a;
static void signal_handler(int sig, siginfo_t *si, void *unused)
{
int b;
................
b = ...;
a = a | b;
................
}
Unless your program is multithreaded, signal handlers never run concurrently with other code in your program, and they certainly never run concurrently with the code they've interrupted. Your code is fine as long as the signal sig
is masked for the duration of the signal handler.
来源:https://stackoverflow.com/questions/8488791/proper-usage-of-volatile-sig-atomic-t