title: Best way to exit application at CTRL+C interrupt on Linux. (C/C++)
body: <p>My application checks for user input in its main thread:</p>
  
  <pre><code>while (running)
  {
      std::string console;
      if (std::getline(std::cin, console))
      {
          process(&amp;console);
      }
  }
  </code></pre>
  
  <p>Before that I have setup a sigaction to detect CTRL+C in conjunction with a function handler to shutdown other threads. 
  Now, when a SIGINT occurs the application crashes; GDB output:
  <img src="https://i.stack.imgur.com/3Rfaj.png" alt="enter image description here"></p>
  
  <p>I was looking around and found other solutions such as non-blocking input reading: (pseudo-code)</p>
  
  <pre><code>while (running)
  {
      if (input_avail())
      {
          getinput
          process
      }
      else
          sleep(1);
  }
  </code></pre>
  
  <p>But even that fails for me at the sleep function (nanosleep):
  <img src="https://i.stack.imgur.com/5wRYp.png" alt="enter image description here"></p>
  
  <p>So I'm quite curious on how other people achieve this?</p>
  
  <p>(Using g++ v4.8.2 Kernel 3.10)</p>
  
  <p><strong>Additional info requested:</strong></p>
  
  <p>Before the main thread loop:</p>
  
  <pre><code>struct sigaction sigIntHandler;
  sigIntHandler.sa_handler = signalinfo;
  sigemptyset(&amp;sigIntHandler.sa_mask);
  sigIntHandler.sa_flags = 0;
  
  sigaction(SIGTERM, &amp;sigIntHandler, NULL);
  sigaction(SIGQUIT, &amp;sigIntHandler, NULL);
  sigaction(SIGINT, &amp;sigIntHandler, NULL);
  </code></pre>
  
  <p>Signal handler:</p>
  
  <pre><code>void signalinfo(int signum)
  {
      // Only setting a flag so threads know to exit.
      pCore-&gt;Termination(signum);
  }
  </code></pre>