Last active
January 26, 2017 23:30
-
-
Save bl4ckb0ne/50331a78b3a58fdf32b0fbbae209180c to your computer and use it in GitHub Desktop.
Handle a signal and give it to anything
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <csignal> | |
#include <iostream> | |
#include <thread> | |
namespace | |
{ | |
volatile std::sig_atomic_t gSignalStatus = 0; | |
} | |
class Foo | |
{ | |
public: | |
void run(volatile std::sig_atomic_t& sig_status) | |
{ | |
std::cout << "Start Foo::run\n"; | |
while(sig_status == 0) | |
{ | |
continue; | |
} | |
std::cout << "Stop Foo::run\n"; | |
} | |
}; | |
void signal_handler(int signal) | |
{ | |
std::cout << "Signal : " << signal << '\n'; | |
gSignalStatus = signal; | |
} | |
int main() | |
{ | |
// Install a signal handler | |
std::signal(SIGINT, signal_handler); | |
std::cout << "Signal at " << gSignalStatus << '\n'; | |
Foo f; | |
f.run(gSignalStatus); | |
std::cout << "Shutting down\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment