I am running a C++ application on xubuntu and implemented a signal handler which handles SIGINT. My implementation should be fine, since it is working on all other machines (same OS) - except mine. "Working" means that the signal is received by a signal handler thread and a controlled shutdown is executed.
On my machine, ctrl+c just straight kills my application. No handling at all, only a "^C" on the console.
Does anyone have an idea why this behavior is machine dependent? Are there any setting I could check? Thanks.
EDIT: to see if it is related to the terminal ctrl+c behavior I tried kill -2 PID instead of ctrl+c : Same behavior, no handling on my machine, handling as expected on other machines.
Here is a code example of my signal handler:
std::atomic_bool shutdown_requested;
void SignalHandler() {
sigset_t signal_set;
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGTERM);
sigaddset(&signal_set, SIGINT);
while (!shutdown_requested) {
int sig = SIGUNUSED;
sigwait(&signal_set, &sig);
switch (sig) {
case SIGINT:
case SIGTERM:
shutdown_requested = true;
break;
}
}
}
void SetSignalHandler() {
sigset_t signals;
sigfillset(&signals);
pthread_sigmask(SIG_SETMASK, &signals, nullptr);
std::thread(SignalHandler);
}
The atomic shutdown_requested signals the main thread to shut down. Any ideas?