Skip to content

Instantly share code, notes, and snippets.

@xueliu
Created May 2, 2018 14:26
Show Gist options
  • Save xueliu/11122f1b2c609c1ee98fbe633798d540 to your computer and use it in GitHub Desktop.
Save xueliu/11122f1b2c609c1ee98fbe633798d540 to your computer and use it in GitHub Desktop.
using std::function to provide signal handler
#include <functional>
#include <iostream>
namespace {
std::function<void(int)> shutdown_handler;
void signal_handler(int signal) { shutdown_handler(signal); }
} // namespace
int main(int argc, char *argv[]) {
std::signal(SIGINT, signal_handler);
MyTCPServer server;
shutdown_handler = [&](int signal) {
std::cout << "Server shutdown...\n";
server.shutdown();
};
server.do_work_for_ever();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment