Skip to content

Instantly share code, notes, and snippets.

@bl4ckb0ne
Last active January 26, 2017 23:30
Show Gist options
  • Save bl4ckb0ne/50331a78b3a58fdf32b0fbbae209180c to your computer and use it in GitHub Desktop.
Save bl4ckb0ne/50331a78b3a58fdf32b0fbbae209180c to your computer and use it in GitHub Desktop.
Handle a signal and give it to anything
#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