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;
................
}
The definition of sig_atomic_t is: "The type defined is sig_atomic_t which 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 standard clearly states " sig_atomic_t ... is ... an object that can be accessed as an ...
Objects that are declared as volatile are not used in certain optimizations because their values can change at any time. The system always reads the current value of a volatile object when it is requested, even if a previous instruction asked for a value from the same object.
The signal() function does not (necessarily) block other signals from arriving while the current handler is executing; sigaction() can block other signals until the current handler returns. The signal() function (usually) resets the signal action back to SIG_DFL (default) for almost all signals.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With