title: Graceful shutdown on SIGINT
body: <p>I would like to handle SIGINT signal from the kernel in order to call a function that grecefully shutdown my process.<br>
  Here's a working example, a thread is waiting the signal and then call the function handle_stop:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;boost/asio.hpp&gt;
  #include &lt;boost/thread.hpp&gt;
  
  void handle_stop(const boost::system::error_code&amp; error, int signal_number) {
      std::cout&lt;&lt;"Executing Safe Shutdown"&lt;&lt;std::cout;
      exit(0);
  }
  
  int main() {
      std::cout&lt;&lt;"Init"&lt;&lt;std::cout;
      boost::asio::io_service signalService;
      boost::asio::signal_set signals(signalService, SIGINT, SIGTERM, SIGQUIT);
      signals.async_wait(handle_stop);
      boost::thread signalThread(boost::bind(&amp;boost::asio::io_service::run, &amp;signalService));
  
      std::cout&lt;&lt;"Starting programm"&lt;&lt;std::cout;
      while (true) {
          std::cout&lt;&lt;"Waiting ctl-c"&lt;&lt;std::cout;
          sleep(1);
      }
  }
  </code></pre>
  
  <p>My goal is pack inside a class the thread and the function to call for shut the process down.<br>
  Here's an non working attempt, <strong>the process shutdown immediately without wait for the signal</strong>.<br>
  What is wrong?</p>
  
  <pre><code>#include &lt;atomic&gt;
  #include &lt;iostream&gt;
  #include &lt;boost/asio.hpp&gt;
  #include &lt;boost/thread.hpp&gt;
  
  class Shutdown {
  public:
        Shutdown():is_signal_received_ (false) {
                //signals_::remove();
                std::cout&lt;&lt;"constructor"&lt;&lt;std::endl;
        }
        ~Shutdown() {
        }
  
        void init() {
                std::cout&lt;&lt;"Init "&lt;&lt;std::endl;
                boost::asio::signal_set signals(signalService_, SIGINT, SIGTERM, SIGQUIT);
                signals.async_wait(boost::bind(boost::mem_fn(&amp;Shutdown::handleStop), this, _1, _2));
                boost::thread signalThread(boost::bind(&amp;boost::asio::io_service::run, &amp;signalService_));
                std::cout&lt;&lt;"Init Completed"&lt;&lt;std::endl;
        }
  
        bool isSignalReceived() const {
                return is_signal_received_;
        }
  
  private:
        std::atomic&lt;bool&gt; is_signal_received_;
        boost::asio::io_service signalService_;
  
        void handleStop(const boost::system::error_code&amp; error, int signal_number) {
                is_signal_received_ = true;
                myHandleStop(error, signal_number);
        }
  
        virtual void myHandleStop(const boost::system::error_code&amp; error, int signal_number) {
        }
  };
  
  class MyShutdown: public Shutdown {
  private:
        void myHandleStop(const boost::system::error_code&amp; error, int signal_number) {
                std::cout&lt;&lt;"Executing Safe Shutdown"&lt;&lt;std::cout;
                exit(0);
        }
  };
  
  int main() {
      MyShutdown safeShutdown;
      safeShutdown.init();
      while (true) {
          std::cout&lt;&lt;"Waiting ctl-c"&lt;&lt;std::cout;
          sleep(1);
      }
  }
  </code></pre>
  
  <p>Here's the command for compiling:</p>
  
  <blockquote>
    <p>g++ -o main main.cpp -lpthread -l boost_thread -l boost_system --std=c++11  </p>
  </blockquote>