Skip to content

Instantly share code, notes, and snippets.

@alepez
Last active June 27, 2017 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alepez/61c02a6f031ce60f0b3e19ae27ffd5db to your computer and use it in GitHub Desktop.
Save alepez/61c02a6f031ce60f0b3e19ae27ffd5db to your computer and use it in GitHub Desktop.
/*
* 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