Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
/*
* Example 1: exits on SIGINT
*/
#include <iostream>
#include <signal.h>
#include <unistd.h>
bool continueRunning = true;
void sighandler(int signum) {
continueRunning = false;
}
int main() {
signal(SIGINT, sighandler);
while (continueRunning) {
usleep(1000);
}
std::cout << "exit" << std::endl;
return 0;
}
/*
* Example 1: does not exit on SIGINT
*/
#include <iostream>
#include <signal.h>
#include <unistd.h>
bool continueRunning = true;
void sighandler(int signum) {
continueRunning = false;
}
int main() {
signal(SIGINT, sighandler);
while (continueRunning) {
}
std::cout << "exit" << std::endl;
return 0;
}
/*
* Example 1: with volatile, exits on SIGINT
*/
#include <iostream>
#include <signal.h>
#include <unistd.h>
volatile bool continueRunning = true;
void sighandler(int signum) {
continueRunning = false;
}
int main() {
signal(SIGINT, sighandler);
while (continueRunning) {
}
std::cout << "exit" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment