title: background function c++ with thread
body: <p>here is my code:</p>
  
  <pre><code>#include &lt;wiringPi.h&gt;
  
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;unistd.h&gt;
  #include &lt;thread&gt;
  
  using namespace std;
  
  bool running;
  const int pin = 1; // Pin 18 = Pin 1 in WiringPi
  
  void stayAwake() {
      while(running) {
          digitalWrite(pin, 1);
          usleep(1000);
          digitalWrite(pin, 0);
          usleep(1000);
      }
  }
  
  int main() {
      if (wiringPiSetup() == -1)
          return 1;
  
      pinMode(pin, OUTPUT);
  
      running = true;
      thread t(stayAwake);
  
      while(1) {
          int input = 0;
          cout &lt;&lt; "put in a freq between 1000 and 2000:";
          while(input &lt; 1000 || input &gt; 2000) cin &gt;&gt; input;
  
          running = false;
          t.join();
          for(int i=0; i&lt;=1000; i++) {
              digitalWrite(pin, 1);
              usleep(input);
              digitalWrite(pin, 0);
              usleep(1000);
          }
  
          running = true;
          thread t(stayAwake);
      }
  }
  </code></pre>
  
  <p>I need the "stayAwake" function to run the whole time in the background, until the user enters a valid number, then it should stop and immediately start again, after the for-loop has finished. ANd this as long as I want it. (= until Ctrl-C)</p>
  
  <p>But the program just breaks up with:</p>
  
  <pre><code>terminate called without an active exception
  </code></pre>