Handling Ctrl-C cleanly in C++

// Handling Interrupt Signals in C++
//
// Zaytsev Roman Borisovich <moc.liamg|hcivosirob.vestyaz.namor#moc.liamg|hcivosirob.vestyaz.namor>

#include <iostream>
#include <signal.h>
#include <zmq.hpp>

static int s_interrupted = 0;
static void s_signal_handler (int signal_value)
{
s_interrupted = 1;
}

static void s_catch_signals (void)
{
struct sigaction action;
action.sa_handler = s_signal_handler;
action.sa_flags = 0;
sigemptyset (&action.sa_mask);
sigaction (SIGINT, &action, NULL);
sigaction (SIGTERM, &action, NULL);
}

int main (void)
{
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REP);
socket.bind ("tcp://*:5555");

s_catch_signals ();
while ( true ) {
// Blocking read will throw on a signal
zmq::message_t msg;
try {
socket.recv (&msg);
}
catch(zmq::error_t& e) {
std::cout << "W: interrupt received, proceeding…" << std::endl;
}
if (s_interrupted) {
std::cout << "W: interrupt received, killing server…" << std::endl;
break;
}
}
return 0;
}