title: Running a periodic loop in background in C++/C
body: <p>I'm trying to create C++ program in the sense of embedded hardware programs that work in real time. The main loop in my C++ program uses a delay time of 250milliseconds. It's like:</p>
  
  <pre><code>int main()
  {
    do{
     doSomething();
     delay(250);
    }while(1)
  }
  </code></pre>
  
  <p>The delay in main loop is crucial for my program to operate. 
  I need to check something else using 5ms delays.</p>
  
  <pre><code>sideJob()
  {
     while(1){
      checkSomething();
      delay(5);
     }
  }
  </code></pre>
  
  <p>How do I define the function sideJob to run at the same with the main loop. All in all, I need to get the hang of threading by using, if possible, simple functions. I'm using Linux. Any help will be greately appreaciated.</p>
  
  <p><strong>EDIT:</strong> This is what I got so far, But I want to run the sideJob and main thread at the same time.</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;thread&gt;
  
  using namespace std;
  
  //The function we want to make the thread run.
  void task1(string msg)
  {
  
       cout &lt;&lt; "sideJob Running " &lt;&lt; msg;
  
  }
  
  int main()
  {  
      // Constructs the new thread and runs it. Does not block execution.
      thread t1(task1, "Hello");
  
      //Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
      t1.join();
      while(1){
          printf("Continuous Job\n");   
      }
  }
  </code></pre>