In C ++ 11, I have several functions that should stop as soon as possible (and return to the caller) every time this unix signal is received. At first glance, the exception that occurs when a signal is received and intercepted only in the callerβs function seems an obvious solution.
void sighandler(int sig) { throw new myexc(); } void caller(void) { try { callee1(); callee2(); } catch (myexc e) { ... } }
But safe portable signal processing is quite limited, since changing the value of volatile sig_atomic_t seems to be the only correct one in the signal handler. But I don't want my code to get littered with checking if the sig_atomic_t file has changed or not.
void sighandler(int sig) { vol = 1; } void callee1(void) { do_stuff(); if (vol == 1) return; do_other_stuff(); if (vol == 1) return; do_something_again(); ... }
Having a thread waiting for a change in value, then throwing an exception that must be caught by another thread is also not an acceptable solution.
How can I do this in a safe, portable and elegant way?
source share