title: C++ main program with a thread requires Ctrl+C twice in order to exit
body: <p>I am running the below C++ program, with a thread inside it on Ubuntu.<br>
  Why does it require two Ctrl+C calls in order to quit the program?<br>
  I notice that the first Ctrl+C I issue to it only closes myThread.</p>
  
  <p>NOTE: I am sure that myThread thread ceases to exist after the first Ctrl+C. This I found by doing ps aux. And I get the print <code>main_recv_sigint: Executed</code> only after the second Ctrl+C<br>
  In my production code, I am excuting a python code instead of <code>sleep 1000</code></p>
  
  <p>How can I have a single Ctrl+C close it completely?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;signal.h&gt;
  #include &lt;thread&gt;
  #include &lt;cstdlib&gt;
  #include "unistd.h"
  #include &lt;cstdio&gt;
  
  void *myThread(void *params)
  {
      std::string cmd = "sleep 1000";
      if (system (cmd.c_str ()));
  
      return NULL;
  }
  
  void main_recv_sigint (int sig);
  bool runMainThread = true;
  
  int main(int argc, char *argv[])
  {
      pthread_t threadID;
      pthread_create(&amp;threadID, NULL, myThread, NULL);
  
      signal(SIGINT, main_recv_sigint);
  
      while (runMainThread) {
          while (runMainThread)
              sleep(2);
  
          if (!runMainThread)
              break;
      }
  
      printf("Waiting for myThread() to exit\n");
      pthread_join(threadID, NULL);
      printf("Wait for myThread() exit done\n");
  
      return 0;
  }
  
  void main_recv_sigint (int sig)
  {
      printf("main_recv_sigint: Executed\n");
      runMainThread = false;
      return;
  }
  </code></pre>